Python selenium parent and child, brother, and adjacent node locating methods, pythonselenium

Source: Internet
Author: User

Python selenium parent and child, brother, and adjacent node locating methods, pythonselenium

Today, I want to share with you the selenium Method for locating parent and child nodes, brothers, and adjacent nodes. Many people may find that the node they want to locate cannot be directly located in actual applications, it is easy to locate the child node from the parent node, but it is difficult to locate the child node from the child node. Don't worry, let's take a look at the step-by-step explanation by the blogger.

1. Locate the child node from the parent node

The simplest thing to do is to locate the child node by the parent node. We have many methods to locate the child node. The following is an example:

Run the following code:

<Html> <body> <div id = "A"> <! -- Parent node locates child nodes --> <div id = "B"> <div> parent to child </div> </body> </ html>

To locate a subnode without id based on Node B, the code example is as follows:

#-*-Coding: UTF-8-*-from selenium import webdriverdriver = webdriver. firefox () driver. get ('d: \ py \ AutoTestFramework \ src \ others \ test.html ') #1. search for print driver in series. find_element_by_id ('B '). find_element_by_tag_name ('div '). text #2. find the print driver for the xpath parent-child relationship. find_element_by_xpath ("// div [@ id = 'B']/div "). text # 3.css selector parent-child relationship search for print driver. find_element_by_css_selector ('div # B> div '). text # 4.css selector nth-childprint driver. find_element_by_css_selector ('div # B div: nth-child (1 )'). text # 5.css selector nth-of-typeprint driver. find_element_by_css_selector ('div # B div: nth-of-type (1 )'). text #6. xpath axis childprint driver. find_element_by_xpath ("// div [@ id = 'B']/child: div "). textdriver. quit ()

Result:

Parent to child
Parent to child
Parent to child
Parent to child
Parent to child
Parent to child

From 1st to 3rd is a method we are familiar. 4th methods use the css selector nth-child (n). The selector returns the nth node, which is a div tag. 5th methods use another css selector: nth-of-type (n), the selector returns the nth div tag, note the difference with the previous selector; 6th methods use the xpath axis child, this is the default axis of xpath, which can be ignored without writing. Its essence is the same as that of method 2.

Of course, there are also some selectors in css that can select parent-child relationships, such as last-child and nth-last-child. If you are interested, you can go to Baidu and have the opportunity to talk about css selector.

2. Parent nodes are located by Child Nodes

It is a little difficult for a child node to locate the parent node. The following code:

<Html> <body> <div id = "A"> <! -- Child nodes locate parent nodes --> <div> child to parent <div> <div id = "C"> </div> </div> </body> 

We want the C node to locate the div of its two-layer parent node. The sample code is as follows:

#-*-Coding: UTF-8-*-from selenium import webdriverdriver = webdriver. firefox () driver. get ('d: \ py \ AutoTestFramework \ src \ others \ test.html ') #1. xpath :'. 'indicates the current node ;'.. 'indicates the parent node print driver. find_element_by_xpath ("// div [@ id = 'C']/.. /.. "). text #2. xpath axis parentprint driver. find_element_by_xpath ("// div [@ id = 'C']/parent: */parent: div "). textdriver. quit ()

Result:

Child to parent
Child to parent

Here we have two methods: 1st is .. as we know ,. indicates the current node ,.. it indicates the parent node. In the same way as above, it is a parent in the xpath axis and obtains the parent node of the current node. This is also a pain point of css selector, because css design does not allow methods to obtain the parent node (at least not currently)

3. Locate the brother node on the younger brother Node

This is 3rd and 4th cases. We want to locate the sibling node here. See the following source code:

<Html> <body> <div id = "A"> <! -- The following two nodes are used to locate sibling nodes --> <div> brother 1 </div> <div id = "D"> </div> <div> brother 2 </div> </div> </body> 

How can I locate my brother node through node D? Sample Code:

#-*-Coding: UTF-8-*-from selenium import webdriverdriver = webdriver. firefox () driver. get ('d: \ Code \ py \ AutoTestFramework \ src \ others \ test.html ') #1. xpath: Obtain print driver from the parent node. find_element_by_xpath ("// div [@ id = 'D']/.. /div [1] "). text #2. xpath axis preceding-siblinuplint driver. find_element_by_xpath ("// div [@ id = 'D']/preceding-sibling: div [1]"). textdriver. quit ()

Result

Brother 1
Brother 1

Here, the blogger also lists two methods: one is to obtain the brother node through the parent node of the node, the other is more elegant, through the xpath axis: preceding-sibling, it can obtain all brother nodes at the same level of the current node. Note the number in the brackets. 1 indicates a brother node closest to the current node. The larger the number, the farther it is from the current node. Of course, the xpath axis: preceding can also be used, but it is complicated to use. It obtains all non-ancestor nodes before the node (which is not well explained here, I will write a blog post to explain all the axes in the next day)

4. Locate the younger brother node by the brother Node

The source code is consistent with 3. To use node D to locate its younger brother node, see the code example:

#-*-Coding: UTF-8-*-from selenium import webdriverdriver = webdriver. firefox () driver. get ('d: \ Code \ py \ AutoTestFramework \ src \ others \ test.html ') #1. xpath: Obtain print driver from the parent node. find_element_by_xpath ("// div [@ id = 'D']/.. /div [3] "). text #2. xpath axis following-siblinuplint driver. find_element_by_xpath ("// div [@ id = 'D']/following-sibling: div [1]"). text #3. xpath axis followinuplint driver. find_element_by_xp Ath ("// div [@ id = 'D']/following ::*"). text # 4.css selector + print driver. find_element_by_css_selector ('div # D + div '). text # 5.css selector ~ Print driver. find_element_by_css_selector ('div # D ~ Div '). textdriver. quit ()

Result:

Brother 2
Brother 2
Brother 2
Brother 2
Brother 2

The blogger shared five methods to locate his younger brother node. The above three methods use xpath. The first is a good understanding, and the second is the xpath axis: following-sibling, which is similar to preceding-sibling, it is used to obtain all younger brother nodes at the same level of the current node. Similarly, 1 indicates the younger brother node closest to the current node. A larger number indicates the farther away from the current node; the third method uses the xpath axis: following. After the node is obtained, all nodes except the ancestor nodes (the direction is opposite to the preceding node, but it is easy to read and error due to the following order, so it can also be used to obtain the younger brother node, but it is not recommended to use it); fourth and fifth, we use css selector, ++ and ~ The difference is: + indicates the div node that follows the current node ,~ It indicates the div node after the current node. If find_elements is used, a group of div nodes can be obtained.

The above is a detailed description of the Python selenium parent and child, brother, and adjacent node locating methods described in the editor. I hope it will help you. If you have any questions, please leave a message, the editor will reply to you in a timely manner, and I would like to thank you for your support for the help House website!

Related Article

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.