Java Selenium XPath Localization implementation method _java

Source: Internet
Author: User
Tags xpath

XPath's positioning method, very powerful. Using this method, you can almost navigate to any element on the page.

Reading Table of Contents

    1. What is XPath
    2. Disadvantages of XPath positioning
    3. The testxpath.html code is as follows
    4. Absolute path Positioning method
    5. Using the browser debugging tool, you can get the XPath statement directly
    6. Disadvantages of absolute paths
    7. The difference between absolute and relative paths
    8. Relative path Positioning method
    9. Use index number to locate
    10. Use Page properties to locate
    11. Fuzzy positioning Starts-with Keywords
    12. Fuzzy positioning contains keywords
    13. Text () function textual positioning

What is XPath

XPath is the abbreviation for XML path, because the HTML document itself is a standard XML page, so we can use XPath to locate page elements.

Disadvantages of XPath positioning

XPath this way of positioning, Webdriver will scan all the elements of the entire page to locate the elements we need, which is a very time-consuming operation, if the script in a large number of XPath to do element positioning, the script can perform slightly slower

The testxpath.html code is as follows

 
 

Absolute path Positioning method

In the tested Web page, find the button in the first div tag

An expression of an XPath

/html/body/div/input[@value = "Query"]

webelement button = driver.findelement (By.xpath ("/html/body/div/input[@ Value= ' query ']);

Using the browser debugging tool, you can get the XPath statement directly

Disadvantages of absolute paths

1. Once the page structure has changed, the path is also invalidated, and must be renewed. So it's not recommended to use absolute paths.

The difference between absolute and relative paths

Absolute path begins with "/", allowing XPath to start parsing from the root node of the document

The relative path starts with "//" and lets XPath parse from any element node in the document

Relative path Positioning method

In the tested Web page, find the button in the first div tag

An expression of an XPath

input[@value = "Query"]

webelement button = driver.findelement (By.xpath ("//input[@value = ' query ')");

Use index number to locate

In the tested Web page, look for the query button in the second div tag

INPUT[2] 

webelement button = driver.findelement (By.xpath ("//input[2]"));

Use Page properties to locate

Locate the first picture element in the page being tested

img[@alt = ' div1-img1 ']

webelement button = driver.findelement (By.xpath ("//img[@alt = ' div1-img1 ')");
 

Fuzzy positioning Starts-with Keywords

Find pictures alt attribute start position contains elements of ' DIV1 ' keyword

Imag[starts-with (@alt, ' div ')]

Fuzzy positioning contains keywords

Find Pictures Alt property contains elements of ' G1 ' keyword

Imag[contains (@alt, ' G1 ')]

Text () function textual positioning

Find all elements of the text as "Baidu search"

Driver.findelement (By.xpath ("//*[text () = ' Baidu search ')");

Find hyperlinks to search for all text

Driver.findelement (By.xpath ("//a[contains (Text (), ' Search ')"));

The above is the Java Selenium XPath positioning of the data collation, follow-up to continue to collate relevant information, thank you for your support for this site!

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.