Selenium--xpath positioning

Source: Internet
Author: User

<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

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.