XPath's positioning method, very powerful. Using this method, you can almost navigate to any element on the page.
Reading Table of Contents
- What is XPath
- Disadvantages of XPath positioning
- The testxpath.html code is as follows
- Absolute path Positioning method
- Using the browser debugging tool, you can get the XPath statement directly
- Disadvantages of absolute paths
- The difference between absolute and relative paths
- Relative path Positioning method
- Use index number to locate
- Use Page properties to locate
- Fuzzy positioning Starts-with Keywords
- Fuzzy positioning contains keywords
- 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!