Switch
When we are in UI automation testing, there will always be a new tab page, a browser-level pop-up box or an iframe tag will appear At this time, we can’t handle these situations with the Api interface provided by WebDriver. You need to use the switch_to module provided by Selenium separately
reference path
# The first way can be operated by directly importing the SwitchTo module from selenium.webdriver.remote.switch_to import SwitchTo # The second way is to operate directly through Webdriver's switch_to driver.switch_to
In fact, webdriver has already encapsulated for us in the previous version to switch Windows, Alert, Iframe, and it can still be used now, but it will be marked with a horizontal line. It means that it is out of date, and it is recommended to use the SwitchTo class to operate.
SwitchToWindows
handles = driver.window_handles # SwitchToWindows accepts the handle of the browser TAB driver.switch_to.window(handles[1])
SwitchToFrame
# SwitchToFrame supports id , Name, element of frame # Accept the element of the positioned iframe, so that it can be positioned by any positioning method frameElement = driver.find_element_by_name(‘top-frame’) driver.switch_to.frame(frameElement) # Locate through the name and id attributes of fame driver.switch_to.frame(‘top-frame’) # When there are multiple levels of iframe nesting, you need to switch the search layer by layer, otherwise it will not be found driver.switch_to.frame(‘top-frame’) driver.switch_to.frame(‘baidu-frame’) # Jump to the outermost page driver.switch_to.default_content() # When multi-layer Iframe, jump to the previous iframe driver.switch_to.parent_frame()
SwitchToAlert
# alert actually It is also a module of Selenium from selenium.webdriver.common.alert import Alert # It can also be called by Webdriver's switch_to # Click the confirm button driver.switch_to.alert.accept() # If it is a confirmation popup, it is equivalent to clicking the need and X buttons driver.switch_to.alert.dismiss() # If there is a text box on the alert, you can enter text. (Note: never encountered) driver.switch_to.alert.send_keys() # Return the text content above Alert text = driver.switch_to.alert.text
Select
In the process of UI automation testing, we often encounter some drop-down boxes. If we Based on Webdriver operation, you need to click twice, and it is prone to problems. In fact, Selenium provides us with a special Select (drop-down box processing module).
Reference path
from selenium.webdriver.support. select import Select
Select operation
# Via select The index of the option to locate the corresponding option (counting from 0) Select(s).select_by_index(5) # Locate by the value attribute value of the option Select(s).select_by_value(‘2‘) # Locate by the text content of the option Select(s).select_by_visible_text(‘Mudanjiang’) # Return the first selected optionElement object Select(s).first_selected_option # Return all selected optionElement objects Select(s).all_selected_options # Cancel all selected options Select(s).deselect_all() # Cancel the corresponding option through the index of the option Select(s).deselect_by_index(1) # Cancel the corresponding option through the value attribute Select(s).deselect_by_value(‘‘) # Cancel the corresponding option through the text content of the option Select(s).deselect_by_visible_text(‘‘)
# The first method can be operated by directly importing the SwitchTo module from selenium.webdriver.remote.switch_to import SwitchTo # The second way is to operate directly through Webdriver's switch_to driver.switch_to
handles = driver.window_handles # SwitchToWindows accepts the handle of the browser TAB driver.switch_to.window(handles[1])
# SwitchToFrame supports id, name, frame elements # Accept the element of the positioned iframe, so that it can be positioned by any positioning method frameElement = driver.find_element_by_name(‘top-frame’) driver.switch_to.frame(frameElement) # Locate through the name and id attributes of fame driver.switch_to.frame(‘top-frame’) # When there are multiple levels of iframe nesting, you need to switch the search layer by layer, otherwise it will not be found driver.switch_to.frame(‘top-frame’) driver.switch_to.frame(‘baidu-frame’) # Jump to the outermost page driver.switch_to.default_content() # When multi-layer Iframe, jump to the previous iframe driver.switch_to.parent_frame()
# alert is actually a module of Selenium from selenium.webdriver.common.alert import Alert # It can also be called by Webdriver's switch_to # Click the confirm button driver.switch_to.alert.accept() # If it is a confirmation popup, it is equivalent to clicking the need and X buttons driver.switch_to.alert.dismiss() # If there is a text box on the alert, you can enter text. (Note: never encountered) driver.switch_to.alert.send_keys() # Return the text content above Alert text = driver.switch_to.alert.text
from selenium.webdriver.support.select import Select
# Use the index of the select option to locate and select the corresponding option (counting from 0) Select(s).select_by_index(5) # Locate by the value attribute value of the option Select(s).select_by_value(‘2‘) # Locate by the text content of the option Select(s).select_by_visible_text(‘Mudanjiang’) # Return the first selected optionElement object Select(s).first_selected_option # Return all selected optionElement objects Select(s).all_selected_options # Cancel all selected options Select(s).deselect_all() # Cancel the corresponding option through the index of the option Select(s).deselect_by_index(1) # Cancel the corresponding option through the value attribute Select(s).deselect_by_value(‘‘) # Cancel the corresponding option through the text content of the option Select(s).deselect_by_visible_text(‘‘)