1. Locate the object in the frame:
Frame nested applications often appear in web applications. Assuming that there are two frames A and B on the page, where B is in A, the content in B needs to go to A first and then to B. Switch_ to_ The frame method can switch the currently located topic to the frame. In fact, another page is nested in the frame, and webdriver can only be recognized on one page at A time, so switch is needed_ to_ Frame method to get the nested pages in the frame.
driver.switch_to.frame("frame1")#Move to id by frame1of frame upper
driver.switch_to.default_content()#Switch the identified subject out frame
# switch_to.frame The parameter must be id Or name,So one frame As long as there is id and name It's easy to handle. If not, there are two solutions:
# 1,Let development add id perhaps name
# 2,use xpath And so on, and then realize the jump
2. alert/confirm/prompt processing:
Use switchto The alert () method locates the current alert/confirm/prompt, and then calls the alert method to operate. Alert provides the following methods:
Text: returns the text content in alert/confirm/prompt
accept: click the confirm button
Disass: click the Cancel button, if there is a cancel button
sendKeys: enter text into prompt
#alert dialog box
driver.find_element(By.XPATH,'//input[@name="alterbutton"]').click()
alert=driver.switch_to.alert #Switch to js Popup
value=alert.text#take out alert Value of
print(value)
alert.accept()#Click OK
#promptbutton dialog box
driver.find_element(By.XPATH,'//input[@name="promptbutton"]').click()
driver.switch_to.alert.send_keys("use Baidu Search")
driver.switch_to.alert.accept()
#confirm dialog box
driver.find_element(By.XPATH,'//input[@name="confirmbutton"]').click()
driver.switch_to.alert.dismiss()#Click Cancel
value=driver.switch_to.alert.text
print(value)
driver.switch_to.alert.accept()
3. Drop down box processing:
There are often drop-down boxes on web pages. The processing of drop-down boxes is relatively simple, which is generally divided into two cases:
(1) The drop-down box is identified by element positioning
driver.find_element(By.XPATH,'//option[@value="orange"]').click()
(2) Create a Select object and process it through the corresponding method
from selenium.webdriver.support.select import Select
element=driver.find_element(By.XPATH,'//select[@id="Selector"]')
select_el=Select(element)
select_el.select_by_value('grape')#value The value of the property
time.sleep(1)
select_el.select_by_visible_text("Banana")#Visible text content
time.sleep(1)
select_el.select_by_index(1) #Index positioning, index starts from 0
4. Call javascript
When webdriver encounters an operation that cannot be completed, javascript can be used to complete it. Webdriver provides execute_script() interface to call js code.
There are two scenarios for executing js: one is to execute js directly on the page, and the other is to execute js on a located element
driver.execute_script('alert("hello!!"); ') #Pop up window
#Border usage: judge whether the element is recognized
element=driver.find_element(By.XPATH,'//input[@name="attach[]"]')
js_str2='arguments[0].style.border="5px solid red"'
driver.execute_script(js_str2,element)
#Browser scroll bar js Browser compatible
#Example: scroll up and down the browser
js_str3='document.body.scrollTop=%d;'
for i in range(9):
time.sleep(2)
if i%2==0:
driver.execute_script(js_str3%5000)
else:
driver.execute_script(js_str3%-5000)
#To remove an element value attribute
element1=driver.find_element(By.XPATH,'//input[@id="showSelectResult"]')
js_str4='arguments[0].removeAttribute("value");'
driver.execute_script(js_str4,element1)
#Sets the of the element value attribute
element2=driver.find_element(By.XPATH,'//input[@id="showSelectResult"]')
js_str5='arguments[0].setAttribute("value","resetvalue");'
driver.execute_script(js_str5,element2)
# Identify and get object properties
driver.get('http://www.baidu.com')
js_str6 = 'var user_input = document.getElementById("su").getAttribute("value");return user_input;'
value=driver.execute_script(js_str6)
print(value)
5. Browser multi window processing
When testing a web application, we sometimes have multiple browser windows. webdriver provides corresponding solutions as follows:
First, we should obtain the unique identifier (handle) of each window, and distinguish different windows through the obtained handle, so as to operate the elements on different windows.
def switch_window_by_title(title):
for handle in driver.window_handles:
driver.switch_to.window(handle)
if driver.title.__contains__(title):
break
def switch_window_by_url(url):
for handle in driver.window_handles:#Get handles to all windows
driver.switch_to.window(handle)#Jump window by handle
if driver.title.__contains__(url):
break
e=driver.find_element(By.XPATH,'//select[@id="jumpMenu"]')
select_e=Select(e)
select_e.select_by_visible_text('Kaifeng Education Network')
# switch_window_by_title('Kaifeng Education Network')
switch_window_by_url('http://jtj.kaifeng.gov.cn/')
driver.find_element(By.XPATH,'//a[text() = "government service"]').click()
6. cookie handling:
If we need to verify whether there are cookies in the browser, because real cookies cannot pass the white box and integration test, webdriver can read, add and delete cookie information
webdriver operates cookie s as follows:
get_cookies() get all cookie information
get_cookie(name) returns cookie information for a specific name
add_cookie(cookie_dict) to add a cookie, it must have name and value values
delete_cookie(name) deletes the cookie information of a specific part
delete_all_cookies() deletes all cookie information