selenium2 element Positioning XPath and Cssselector

Source: Internet
Author: User
Tags xpath

There are several methods of locating the elements in Selenium2,

Commonly used are Id,xpath, cssselector

XPath Introduction:

XPath is a selector
? XPath is validated with Firepath in Firefox
? XPath common symbol description

/represents an absolute path, which is the beginning of the root directory

Represents a relative path

. Represents the current layer

.. Represents a previous layer

* indicates a wildcard character

@ denotes property

The conditional expression of the [] property

A variety of XPath methods directly on the Baidu homepage to operate, please refer to.

<1>/html/div, no node can be selected, because/if used in the middle, represents the absolute path, is the child node of the previous node, and the child of the HTML is head and body.

<2>/html//div, which represents the selection of a div node in all descendant nodes under the root directory,//indicates a relative path

<3>//div, to select all DIV nodes, you can think/html//div and//div Why the result is the same!

Because only the descendant nodes of HTML have div, so. The result of/html//div and//div is the same.

<4>//div/div, which represents the node that selects all div nodes in the child nodes that contain div

<5>//div/div/. That represents the node that selects the current layer of the//div/div node, the same as the result of//div/div

<6>//div/div/. , which indicates that the upper node of the//div/div node is selected, that is, a div node is selected, and the child nodes of the div node have div nodes. A little around the mouth, but understand, will come to an epiphany

<7>//div/div/*, which indicates that all child nodes of the//div/div are selected,//div/div will have two matching nodes, but why is there only one//div/div/*? This is because the second//div/div has no child nodes, so it only matches a

<8>//div[@id = ' input '], which means to select a DIV node with id ' input '

<9>//div[@id = ' input ']/input, which indicates that the input child node of the DIV node with id ' input ' is selected

<10>//table//input[@id = ' user '), which indicates that the input node with ID user is selected for future generations of table

<11>//input[@name = ' identity ' and @class = ' Volvo '], some nodes, only one property cannot be located, you must use multiple attributes to combine positioning, with the connector and. This xpath means selecting an input node with name identity and a class of Volvo

<12>//input[@name = ' identity ' or @class = ' Volvo '), this multi-attribute combination uses the or connector, which indicates the choice of a name as identity, Or the class is a Volvo node, so this XPath matches 4 nodes.

<13>//input[@name = ' identity ' or @class = ' Volvo '][1] See Figure 13, we just found out,

input[@name = ' identity ' or @class = ' Volvo ') match 4, we only need the first one, how to do? Add Index://input[@name = ' identity ' or @class = ' Volvo '][1], please note that the index of the XPath starts with 1, not 0, please remember!

<14> take the last one,//input[@name = ' identity ' or @class = ' Volvo '][last ()]

<15>//table//tr//input, this match comes out, there are 14 nodes, but what if we need to take the first one?

It is possible to use://table//tr//input[1], but let's look at the result, the matching node is actually 8, not 1, this is because//table//tr//input[1] is the first to match the//table below all the TR descendant nodes , and then on this basis, and then match the TR node of all descendants of the input node of the first, because of the number of TR, so the result is certainly not a match, but how to match 1? That is to say, we need to put a lot of tr fixed one, and then look at:

TABLE//TR[1]//INPUT[1], this time there is only one matching out of the node, so, please carefully try to figure out the differences in this, carefully understand

Cssselector Selector
? Cssselector in Firefox with Firepath authentication


Cssselector Common Symbol Description

#表示id
. Represents class
> represents child elements, hierarchy
A space also represents a child element, but is all descendant child elements, equivalent to the relative path in XPath

#input Select a node with ID input
?. Volvo chooses the node of the class for Volvo
Div#radio>input Select all the input nodes under the DIV with ID radio
? div#radio input selects all descendants of the input node under the DIV with ID radio
Div#radio>input:nth-of-type (4) Select the 4th Input node under DIV with ID Radio
Div#radio>:nth-child (1) Select the 1th child node under the DIV with ID radio
Div#radio>input:nth-of-type (4) +label Select the Label node next to the 4th input node under the DIV with ID radio
Div#radio>input:nth-of-type (4) ~label selects all label nodes after the 4th input node under the DIV with ID radio

Input. Volvo[name= ' Identity ') Select Class as. The input node for Volvo and name identity
Input[name= ' identity '][type= ' Radio ']:nth-of-type (1) Select the 1th input node with name identity and type radio
? input[name^= ' ident ') Select all input nodes with the name attribute starting with ident
? input[name$= ' entity ') Select all the input nodes of the name attribute ending with ' entity '
? input[name*= ' Enti ') Select all input nodes that contain the Name property of ' Enti '
? div#radio>*:not (Input) Select all child nodes in the sub-node of the DIV with ID radio that are not input
Input:not ([Type=radio]) Select all nodes in the input node that type is not radio

selenium2 element Positioning XPath and Cssselector

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.