Web Automation Test-d3-Learning Note III (Selenium-switch and SELECTAPI interface)

Source: Internet
Author: User

Switch

When we test in UI Automation, we always have to create a new tab page, pop up a browser-level popup, or an IFRAME tag, and we can't handle these situations with the API interface provided by Webdriver. Requires a separate module switch_to module for selenium

Reference Path

12345 # The first method can be operated by importing the SwitchTo module directly from selenium. Webdriver. Remote. switch_to Import SwitchTo # The second way is to operate the driver directly through the Webdriver switch_to . Switch_to

In fact, Webdriver in the previous version has been packaged for us to switch windows, Alert, Iframe, can still be used, but will be underlined, on behalf of his outdated, it is recommended to use the SwitchTo class to operate.

Switchtowindows

1234 handles = driver. Window_handles # Switchtowindows accepts the handle of the browser tab driver. Switch_to. Window(handles[1])

Switchtoframe

123456789101112131415161718 # switchtoframe Support ID, name, element of frame# Accept the element of the target IFRAME so that it can be positioned by any one of the positioning methodsframeelement = driver. Find_element_by_name(' top-frame ') driver. Switch_to. Frame(frameelement) # Location by fame name, ID propertydriver. Switch_to. Frame(' top-frame ') # When there are multiple layers of iframe nesting, you need a layer-by-layer toggle lookup, otherwise you won't finddriver. Switch_to. Frame(' top-frame ') driver. Switch_to. Frame(' baidu-frame ') # Jump to the outermost pagedriver. Switch_to. Default_content() # When a multilayer iframe jumps to the previous layer in the IFRAMEdriver. Switch_to. Parent_frame()

Switchtoalert

1234567891011121314151617 # Alert is actually a module of seleniumfrom selenium. Webdriver. Common. Alert Import alert # can also be called via Webdriver's switch_to# Click the Confirm buttondriver. Switch_to. Alert. Accept() # If it's a confirmation box, it's equivalent to the click and X buttondriver. Switch_to. Alert. Dismiss() # If you have a text box on alert, you can enter text. (Note: not encountered)driver. Switch_to. Alert. Send_keys() # Returns the text content above alerttext = driver. Switch_to. Alert. Text

Select

In the UI Automation test process, often encounter some drop-down boxes, if we are based on webdriver operation, we need click two times, and very prone to problems, in fact, selenium provides us with a special select (dropdown box processing module).

Reference Path

1 From Selenium. Webdriver. Support. Select Import Select

Select operation

1234567891011121314151617181920212223242526 # Position selection by index of select option (counting starting from 0)Select(s). Select_by_index(5) # The value of the option is positionedSelect(s). Select_by_value(' 2 ') # Navigate through the text content of the optionSelect(s). Select_by_visible_text(' Mudanjiang ') # Returns the first selected OptionElement objectSelect(s). First_selected_option # returns all the selected OptionElement objectsSelect(s). All_selected_options # deselect all selected optionSelect(s). Deselect_all() # Cancel the option by using the option indexSelect(s). Deselect_by_index(1) # By the Value property, to cancel the corresponding optionSelect(s). Deselect_by_value(") # Remove the option by using the text content of optionSelect(s). Deselect_by_visible_text(")

Web Automation Test-d3-Learning Note III (Selenium-switch and SELECTAPI interface)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.