<HTML><Head><title>Test Xpath</title></Head><Body> <DivID= "Div1"> <inputname= "Div1input"></input> <ahref= "Http://www.sogou.com">Sogou Search</a> <imgalt= "DIV1-IMG1"src= "Http://www.sogou.com/images/logo/new/sogou.png"href= "Http://www.sogou.com">Sogou Pictures</img> <inputtype= "button"value= "Query"></input> </Div> <BR/> <Divname= "Div2"> <inputname= "Div2iniput" /></input> <ahref= "Http://www.baidu.com">Baidu Search</a> <imgalt= "Div2-img2"src= "Http://www.baidu.comn/img/bdlogo.png"href= "Http:/www.baidu.com">Baidu Pictures</img> </Div></Body></HTML>
1. Absolute path Positioning method
The absolute path of an XPath is primarily used to locate the absolute path of an element with a hierarchical relationship of tag names. The outermost layer is the HTML language, the body text, the first level down search, if there is more than one level of the same tag name, then in the upper and lower order to determine the number of
In the tested Web page, find the button in the first div tag
An XPath expression
/html/body/div/input[@value = "Query"]
Webelement button = driver.findelement (By.xpath ("/html/body/div/input[@value = ' query ']");
Once the page structure has changed, the path is invalidated and must be restarted. So it is not recommended to use absolute path notation
2. Relative path positioning
In the tested Web page, find the button in the first div tag
An XPath expression
input[@value = "Query"],//indicates that the current page is under a directory, input indicates the label name of the anchored element
Webelement button = driver.findelement (By.xpath ("//input[@value = ' query ']");
If you don't want to specify a tag name, you can use an asterisk (*) instead
Webelement button = driver.findelement (By.xpath ("//*[@value = ' query ']");
Using XPath is not limited to the three attribute values of ID, name, and class, and any attribute value of an element can be used as long as it uniquely identifies an element.
3. Combination of hierarchies and attributes
Selenium--xpath positioning