The positioning method of xpath is very powerful. Use this method to locate almost any element on the page.
Reading Contents
What is xpath
xpath is the abbreviation of XML Path. Since the HTML document itself is a standard XML page, we can use the usage of Xpath to locate page elements.
Disadvantages of xpath positioning
This is a very time-consuming operation. If xpath is used to locate all elements of the entire page, the script may be slightly slower to execute.
The testXpath.html code is as follows
<html><head><title>Test Xpath</title></head><body> <div id="div1"> <input name="div1input"></input> <a href="http://www.sogou.com">Sogou Search</a> <img src="http://www.sogou.com/images/logo/new/sogou.png" href="http://www.sogou.com">Sogou Picture</img> <input type="button" value="query"></input> </div> <br /> <div name="div2input" /></input> <a href="http://www.baidu.com">Baidu Search</a> <img src="http://www.baidu.comn/img/bdlogo.png" href="http://www.baidu.com">Baidu Pictures</img> </div></body></html>
Absolute path positioning method
In the tested web page, find the button in the first div tag
XPath expression
/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 directly obtain xpath statements
Disadvantages of absolute paths
1. Once the page structure changes, the path change will also fail and must be re-established. Therefore, it is not recommended to use absolute path writing
The difference between absolute and relative paths
The absolute path starts with "/", let xpath parse from the root node of the document
The relative path starts with "//", let 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
XPath expression
//input[@value="query"]WebElement button = driver.findElement(By.xpath("//input[@value='query']"));Positioning with index numbers
In the tested web page, find the "Query" button in the second div tag
//input[2] WebElement button = driver.findElement(By.xpath("//input[2]"));Use page attributes to locate
Position the first image element in the tested page
//img[@alt='div1-img1']WebElement button = driver.findElement(By.xpath("//img[@alt='div1-img1']"));Fuzzy positioning starts-with keywords
Find elements with the 'div1' keyword at the start position of the image alt attribute
//imag[starts-with(@alt,'div')]
Fuzzy positioning contains keywords
Find elements with the alt attribute containing the 'g1' keyword
//imag[contains(@alt,'g1')]
text() function text positioning
Find all elements with text "Baidu Search"
driver.findElement(By.xpath("//*[text()='Baidu search']"));
Find all hyperlinks with text "Search"
driver.findElement(By.xpath("//a[contains(text(),'search')]"));
The above is the information sorting out the java selenium XPath positioning. We will continue to sort out the relevant information in the future. Thank you for your support for this site!