Switching into three: window switch, iframe switching, frame processing Alert shells (shells block non-HTML) PS ( used here for the HTML file , extraction code: t0n7)
A window switching
1, need to know what window you want to switch the call? - handle. Get all window handle current browser session opens
By: driver.window_handles - put in the "list", according to the sequential window handle storage.
2, switch over: driver.seitch_to.window (window handle).
3, entered a new page, only the operating elements for the new page.
from the Selenium Import webdriver Import Time from selenium.webdriver.support.wait Import WebDriverWait from selenium.webdriver.support Import expected_conditions AS EC from selenium.webdriver.common.by Import By Driver = webdriver.Chrome () driver.get ( " File: ///E:/WebWebpageTest/page.html " ) driver.maximize_window () # maximize browser # positioning of expression, a new page click on the jump elements LOC = (By.XPATH, ' // a [@ the above mentioned id = "the ZCA"] ' ) WebDriverWait (Driver, 30 ) .until (EC.visibility_of_element_located (LOC)) # click on the new page elements appear driver.find_element (* LOC) .click () # a new window pops up # wait for a new window, go to window switching the time.sleep (0.5) # sometimes fail to get the window immediately to the value # acquired window handle all WINS = driver.window_handles Print ( ' all the window handles ' , WINS) # returns the current window handle cur_win = Driver .current_window_handle Print ( ' the current window handle is: ' , cur_win) # switch to open windows latest driver.switch_to.window (wins [-1]) Print ( ' latest window handle is: ' , WINS [-1 ]) # Positioning expression, a new page input element LOC = (By.XPATH, ' // the INPUT [@ the above mentioned id = "userA"] ' ) WebDriverWait (Driver, 30 ) .until (EC.visibility_of_element_located (LOC)) # in the new window enter data driver.find_element (* LOC) .send_keys ( ' this is a new window ' ) # View the results of the time.sleep (3 ) Driver .quit ()
Two, iframe switch
iframe HTML page is nested inside another HTML page,
If the element you want to operate, which in the iframe, it is necessary to switch to the iframe
1, if your element inside the iframe
2, find the iframe
3, switching an iframe
Can be seen by viewing the two paths and a HTML iframe, which will be described in 2 of 3, it is necessary to switch iframe
Switching iframe, supports three parameters pass. 1, ifram Nam elements E e,
2, the subscript iframe element (starting from 0),
3, iframe WebElement corresponding objects (eight elements by positioning element)
from the Selenium Import webdriver Import Time from selenium.webdriver.support.wait Import WebDriverWait from selenium.webdriver.support Import expected_conditions AS EC from selenium.webdriver.common.by Import By Driver = webdriver.Chrome () driver.get ( " File: ///E:/WebWebpageTest/page.html " ) driver.maximize_window () # maximize browser # handover iframe three types of transfer driver.switch_to.frame ( ' myframe1 ' ) #name # driver.switch_to.frame (0) # superscript # driver.switch_to.frame (driver.find_element_by_xpath ( '// iframes [@ ID = "idframe1"]')) by eight targeting # # after the handover is completed, that is, a new HTML page is positioned inside # targeting expression, the page input element LOC = (By.XPATH, ' // iNPUT [@ ID = "userA"] ' ) WebDriverWait (Driver, 30 ) .until (EC.visibility_of_element_located (LOC)) # window input data driver.find_element (* LOC) .send_keys ( ' to internal iframes ' ) # View results the time.sleep (. 3 ) # cut out and switch directly to the default home page, an outermost layer HTML page driver.switch_to.default_content () #Iframes to cut a (nested) # driver.switch_to.parent_frame () LOC = (By.XPATH, ' // INPUT [@ ID = "User"] ' ) WebDriverWait (Driver, 30 ) .until (EC .visibility_of_element_located (LOC)) # window input data driver.find_element (* LOC) .send_keys ( ' switch to the default home page ' ) driver.quit ()
Three, alert processing block bomb
It is not HTML elements, js is the elastic block, the positioning can not be used F12
Operational procedures: non-HTML elements
Use alert categories, resulting in a non-operating elements HTML elements appear
1, switch to it
2, or else close to him
Four elastic frame operation
al.dismiss () # cancel
al.accept () # determines
print (al.text) # acquired pop-up text
send_keys # input popup
from the Selenium Import webdriver Import Time Driver = webdriver.Chrome () driver.get ( " File: /// E: /WebWebpageTest/page.html " ) driver.maximize_window () # maximize browser # Click the pop-up box appears non-HTML driver.find_element_by_id ( ' Alert ' ) .click () the time.sleep ( . 1 ) # switch block needs to receive playing objects returned Al = driver.switch_to.alert # four playing operation box Print (al.text) # Get pop text box # al.dismiss () # cancellation al.accept () # OK # al.send_keys () input popup # # suspend operation to see the time.sleep (. 1 ) driver.quit ()