Selenium private dish Series 2-Use of xpath [zz]

Source: Internet
Author: User
Tags xpath

When writing selenium cases, XPath is rarely used. Now there are a lot of reference materials about the use of xpath. Next I will directly turn to a document about the use of xpath. If you are not familiar with XPath, refer to the following section. You do not need to go to Baidu/Google to search for information about XPath, because the following content is sufficient for you to use when writing tests. If you are familiar with XPath, skip this chapter.

 

XPath syntax(Reprinted from: Http://www.cnblogs.com/jianjialin/archive/2009/02/01/1382056.html

XPath is the XML query language, which is similar to SQL. The XML below is used as an example to introduce the XPath syntax.

<? XML version = "1.0" encoding = "ISO-8859-1"?>
<Catalog>

<CD Country = "USA">
<Title> empire burlesque </title>
<Artist> Bob Dylan </artist>
<Price> 10.90 </price>
</Cd>

<CD Country = "UK">
<Title> hide your heart </title>
<Artist> Bonnie Taylor </artist>
<Price> 9.90 </price>
</Cd>

<CD Country = "USA">
<Title> Greatest Hits </title>
<Artist> Dolly Parton </artist>
<Price> 9.90 </price>
</Cd> </CATALOG>

</CATALOG>

 

Locate a node
XML is a tree structure, similar to the structure of the data folder in the file system. XPath is similar to the path naming method in the file system. However, XPath is a pattern that selects all nodes in the XML file whose path conforms to a pattern. For example, to select all the price elements in CD under catalog, you can use:

/CATALOG/CD/price

If the beginning of xpath is a slash (/), it indicates that this is an absolute path. If there are two diagonal lines (//) at the beginning, all elements in the file that match the pattern will be selected, even at different layers in the tree. The following syntax Selects all elements in the file called Cd (all layers in the tree will be selected ):

// CD

 

Select unknown element
Use the asterisk (wildcards, *) to select unknown elements. The following syntax Selects all child elements of/CATALOG/CD:

/CATALOG/CD /*

The following syntax Selects all child elements of catalog, including price as the child element.

/CATALOG/*/price

The following syntax Selects all elements with two parent nodes called Price.

/*/Price

The following syntax Selects all elements in the file.

//*

Note that to access non-hierarchical elements, the XPath syntax must start with two diagonal lines (//). To access unknown elements, use the asterisk (*), an asterisk can only represent an element with an unknown name, but cannot represent an element with an unknown level.

 

Select Branch
Use brackets to select branch. The following syntax extracts the first element named CD from the child element of catalog. The XPath definition does not contain the 0th element.

/CATALOG/CD [1]

The following syntax selects the last CD element in catalog: (xpathj does not define the first () function. In the preceding example, [1] can be used to retrieve the first element.

/CATALOG/CD [last ()]

The following syntax Selects all/CATALOG/CD elements containing the price sub-element.

/CATALOG/CD [price]

The following syntax Selects all/CATALOG/CD elements whose price value is 10.90.

/CATALOG/CD [price = 10.90]

The following syntax selects the price element of all/CATALOG/CD elements whose value is equal to 10.90.

/CATALOG/CD [price = 10.90]/price

 

Select more than one path
You can select more than one path by using the or operand (|. For example:

/CATALOG/CD/Title | CATALOG/CD/artist

Select All title and artist Elements

// Title | // artist

Select All title, artist, and price elements

// Title | // artist | // price

 

Select attributes
In XPath, you can select attributes in addition to elements. All attributes start. For example, select all the attributes in the file called country.

// @ Country

Select all CD elements containing the country attribute:

// CD [@ Country]

The following syntax Selects all CD elements containing attributes

// CD [@ *]

The following syntax selects the CD element whose country attribute value is UK

// CD [@ Country = 'U']

Select multiple attributes

// CD [@ Country = 'U'] [@ name = 'hyddd ']

Selenium private dish Series 2-Use of xpath [zz]

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.