xpath positioning of the positioning element selenium Detailed

 

xpath selenium provides positioning method name:

driver.find_element_by_xpath (xpath expressions)

xpath is positioned as a whole HTML tree structure. HTML node as a root. Page among nodes and other nodes can have ancestor, there is a relationship between the parents, brothers, and their offspring, similar to the relationship between our human family.

xpath basic orientation grammar

 

 

 

 

 

First, absolute positioning

Features: 1 single slash / beginning; page 2 from the root element (HTML tag) starts, in strict accordance with the position and order of the elements in the HTML page look down

Such as:

driver.find_element_by_xpath("/html/body/div[2]/div[1]/div/div[1]/div/form/span[1]/input")

Second, the relative positioning

Features: 1 with a double slash // at the beginning; 2 does not consider the absolute path and position page elements among; 3 to consider whether there is compliance with the elements of expression can be....

We generally use relative positioning to position elements. To introduce the following expression under the prevailing relative positioning.

 

2.1 Location attribute tag name + node

Syntax: // tag name [@ attribute name = attribute value]

Such as:

Now you want to reference the id "J_password" input element, you can write like this:

ele_password= driver.find_element_by_xpath("//*[@id='J_login_form']/dl/dt/input[@id='J_password']")

Another way to write:

ele_password = driver.find_element_by_xpath("//*[@id='J_login_form']/*/*/input[@id='J_password']")

2.2. Combination element index (subscript) positioned

  Such as:

ele_password = driver.find_element_by_xpath("//*[@id='J_login_form']/*/*/input[2]”)

  

2.3. By the attribute value matching section

Syntax: // tag name [contains (@ attribute name, part of the property value)], // tag name [starts-with (@ attribute name, part of the property value)], // tag name [ends-with (@ attribute name , part attribute value)]

a.starts-with examples: // input [starts-with (@ id, 'ctrl')] Analytical: matching attribute value of the start ctrl

b.ends-with examples: // input [ends-with (@id, '_ userName')] Analytical: userName matching attribute value of the end

c.contains () Examples: // input [contains (@ id, 'userName')] Analytical: userName attribute value matching comprising

as follows:

driver.find_element_by_xpath(“//a[contains(@href, ‘logout’)]”)
driver.find_element_by_xpath(“//a[ends-with(@href, ‘logout’)]”)
driver.find_element_by_xpath(“//a[starts-with(text(), ‘退’)]”)

2.4 The use of text matches

Functions: text ()

Syntax: Text All matches: // tag name [text () = text]

   Text matching section - comprising: // tag name [contains (text (), the text part)]

Sample code is as follows:

driver.find_element_by_xpath ( "// a [text ()," Exit "]") # Full text match
driver.find_element_by_xpath ( "// a [contains (text ()," a ")]) match the text part #

2.5, using the axis targeting expression

Axis computation Name:

ancestor: ancestor node, including the parent node

parent: the parent node

preceding: the label of the current node element ago (before HTML pages) of all nodes

preceding-sibling: the elements of the current node label before all the siblings (same level)

following: the current node label of the element after all the nodes

following-sibling: the label of the current node elements after all sibling nodes (peers)

Using the syntax: Axis :: node name

Consistent with previous positioning before and after, can be separated by /.

E.g:

// div // table / td / preceding :: td / following-sibling :: a // [contains (text (), " course")] 
# indicates all nodes // div // front table / td / path find the node name for the node td, a sibling at the same level downward course contains text

  

 

Guess you like

Origin www.cnblogs.com/123blog/p/12465369.html
Recommended