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 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
- Child selects all children of the current node
- Parent selects the parents of the current node
- Descendant Select all descendant nodes of the current node
- Ancestor Select all ancestor nodes of the current node
- Descendant-or-self selects all descendant nodes of the current node and the current node itself
- Ancestor-or-self selects all ancestors of the current node and the current node itself
- Preceding-sibling all sibling nodes before the current node is selected
- Following-sibling Select all sibling nodes after the current node
- Preceding selects all nodes before the start tag of the current node
- Following select all nodes after the start tag of the current node
- Self Pick Current node
- Attribute selects all properties of the current node
- 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