Use the XPath Contains, sibling function to locate in the selenium Webdriver

Source: Internet
Author: User
Tags xpath xpath contains

Objective

In general, we can navigate to the target element with simple XPath, but it is difficult to navigate in a simple way for some without ID and no name, and other properties are dynamic.

In this case, we need to use xpath1.0 built-in functions for positioning, and here we focus on 3 functions:

    • Contains
    • Sibling
Contains function

With the contains function, we can extract all elements that match a particular text.

For example, on the homepage of Baidu, we use contains to locate elements that contain "news" text.


Baidu_news.png
"//div/a[contains(text(), 新闻)]"

Using XPath contains to locate in Python selenium, the code snippet is as follows:

driver.find_element_by_xpath("//div/a[contains(text(), 新闻)]")
Sibling function

With the sibling function, we can extract all sibling elements of the specified element, that is, all sibling nodes that get the target element.

For example, the "hao123" node is located by the "News" node just now.

"//div/following-sibling::a[contains(text(), 新闻)]"

The Python Selenium code snippet is as follows

driver.find_element_by_xpath(u"//div/a[contains(text(), ‘%s‘)]/following-sibling::*" % u"新闻")

Locate all of its sibling nodes via the "News" node just now.

The Python Selenium code snippet is as follows (note that Find_==elements==_by_xpath is used here):

driver.find_elements_by_xpath(u"//div/a[contains(text(), ‘%s‘)]/following-sibling::*" % u"新闻")

Let's look at a complete code example:

#_ *_ Coding:utf-8 _*___author__ =' Bitter leaves 'From seleniumImport WebdriverImport sysreload (SYS) sys.setdefaultencoding ("Utf-8")if __name__ = =' __main__ ': Driver = webdriver. Ie () Driver.get (u "http://www.baidu.com") # positioning the element containing "news" via contains new_node = Driver.find_element_by_xpath (u"//div/a[contains (Text (), '%s ')] "% u" News ")  Print New_node.text # position the "news" element of the sibling node "hao123" Hao123_node = Driver.find_element_by_xpath ( Span class= "hljs-string" >u "//div/a[contains (Text (), '%s ')]/following-sibling::*"% u "News" ) print hao123_node.text # locate all sibling nodes of the "news" element All_node = Driver.find_elements_by_xpath (u "//div/a[contains (Text (), '%s ')]/following-sibling::*" % u "News") for ee in All_node: print ee.text driver.quit ()          
XPath common functions
    1. Child selects all children of the current node
    2. Parent selects the parents of the current node
    3. Descendant Select all descendant nodes of the current node
    4. Ancestor Select all ancestor nodes of the current node
    5. Descendant-or-self selects all descendant nodes of the current node and the current node itself
    6. Ancestor-or-self selects all ancestors of the current node and the current node itself
    7. Preceding-sibling all sibling nodes before the current node is selected
    8. Following-sibling Select all sibling nodes after the current node
    9. Preceding selects all nodes before the start tag of the current node
    10. Following select all nodes after the start tag of the current node
    11. Self Pick Current node
    12. Attribute selects all properties of the current node
    13. namespace selects all namespace nodes of the current node
Summarize

In this article, the XPath commonly used contains, sibling functions are described and code demonstration, for other functions suggest that you write code to practice, understand its principle, will be more conducive to follow-up automation test practice.

Use the XPath Contains, sibling function to locate in the selenium Webdriver

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.