Web Automation: Selenium principles and element positioning

Source: Internet
Author: User
Tags tag name xpath xpath contains

I. SELENIUM2 webdriver
    • When selenium2.x presents the concept of Webdriver, it provides a completely different way to interact with the browser
    • That is, using the browser's native API, encapsulated into a set of more object-oriented selenium Webdriver API, directly manipulate the elements of the browser page, and even manipulate the browser itself (screenshots, window size, start, close, install plug-ins, configuration certificates, etc.)

    • Thanks to the browser's native API, the speed is greatly improved, and the stability of the call is given to the browser vendor itself, which is obviously more scientific

    • However, some of the side effects are that different browser vendors, the operation and rendering of web elements will be somewhat different, which directly led to the selenium webdriver to separate browser vendors, and provide a different implementation

    • Firefox, for example, has a dedicated firefoxdriver,chrome that has special chromedriver and so on (even Androiddriver and iOS webdriver)

Two. Selenium environment construction and principle explanation

In short, the webdriver in selenium will directly control the browser (via a different browser driver), then test the target->ui level

Three. Common methods and positioning of selenium operation Browser1. Create an Object driver
Driver = Webdriver. Chrome ()
2. Loading URLs in the browser
Driver.get (URL)
3. Maximize the browser window
Driver.maximize_window ()
4. browser window fixed size
Driver.set_window_size (x, y)
5. Forward
Driver.forward ()
6. Back
Driver.back ()
7. Refresh
Driver.refresh ()
8. Screen cutting
Driver.get_screenshot_as_file (filename)
9. Set the wait time: The time unit is S, sometimes the page element loading is not complete, we need to use wait time, wait for the page to load completely
Import timetime.sleep (n)
10. Get the URL of the current page
Driver.current_url
11. Get the title of the current page
Driver.title
12. Exit
Driver.quit ()   to end the process, close all the windows, the end of the final test, we recommend that you use Quitclose ()         to close the current window, when there are more open windows, you can close some windows with close
ID Positioning
DRIVER.FIND_ELEMENT_BY_ID ()
. Class Positioning
Driver.find_element_by_class_name ()
. Name Locator
Driver.find_element_by_name ()
. Tag name Locator
Driver.find_element_by_tag_name ()
. Link text location
Driver.find_element_by_link_text ()           # use all link text to position Driver.find_element_by_partial_link_text ()   # Use part of link text to locate

four. XPath positioning1. Absolute path positioning: Start with "/" and parse from the root node of the document. You can use the Browser debugging tool to get XPath statements directly

2. Disadvantages of absolute paths

When the structure of the page is adjusted, the path also fails, must be rewritten, inconvenient post-maintenance

3. Relative path positioning: Start with "//" and parse from any element node of the document

Other syntax:

An expression Describe
NodeName Select all child nodes of this node
. Select the current node
.. Select the parent node of the current node
@ Select Properties, Example://div[@ Property name = "Property value"]
* Wildcard characters, matching all

Example: Positioning the Baidu input box, according to the attribute Name= "WD" (You can press CTRL + F in the Google Chrome debugging tool to open the positioning input box)

4. XPath positioning and functions and logic
Functions and logic Describe Example
Text () The text content of the element *[@id = "xxx"]//p[text () = "XXXX"]
Contains (@ attribute/text (), value) Include function Contains (@class, "xxx"), contains (text (), "XXXX")

and

Or

Represents conditions and

Represents a condition or

div[@class = "xxx" and contains (@style, "display:visibility")]

Example 1: Using the XPath text () function to locate

Example 2: Using the XPath contains () function to locate

Example 3: Using XPath logic and positioning

5. Pop-up box issues

A few actions of a page, there will be a pop-up box appears, to navigate to the popup will be a few, but through the display value to locate the currently displayed

    • "Display=none" means no pop-up box

    • "Display=block/visibility" indicates a popup box

Example 1: Baidu has a popup box page

Example 2: Baidu page with no popup box

Web Automation: Selenium principles and element positioning

Related Article

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.