positioning web automated testing method, commonly used to locate the relative path, recommended xpath targeting
Positioned in Baidu
# Import library from the Selenium Import webdriver # open the Google browser session is established. Start Chromedriver.exe open Chrome Driver = webdriver.Chrome () # start the Google browser # Driver = webdriver.Firefox () # start Firefox # Driver = webdriver.Ie () # start the IE browser # to access Baidu Home driver. GET ( " http://www.baidu.com " )
First, the eight major positioning:
1. id Positioning
# ID positioning operation attribute + WebElement = ELE1 = driver.find_element_by_id ( " kW " ) Print (ELE1)
2. Label name positioning tag_name, not only to find a specific element
= driver.find_element_by_tag_name ele2 ( " the INPUT " ) # in dom page which, matched to the first element of Print (ele2) ELES = driver.find_elements_by_tag_name ( " the INPUT " ) # list with elements webELement target match all the elements Print ( eles)
3.class_name positioning can not only find a particular element
driver.find_element_by_class_name("s_ipt") driver.find_elements_by_class_name("s_ipt")
4.name positioning is not unique
driver.find_element_by_name("wd")driver.find_elements_by_name("wd")
The above four elements positioned for all the elements
5.link_text positioning, complete matching text values
driver.find_element_by_link_text ( " More " )
6.partial_link_text positioning portion matches
driver.find_element_by_partial_link_text("产品")
7. xpath positioning
grammar
- /:
- Absolute positioning
- //:
- Relative positioning
- Selecting from the current node matches the selected node in the document, regardless of their location
- .:
- Select the current node
- ..:
- Select the parent of the current node
- @:
- Select Properties
- @ Class = "xxx", shuxing property placed in square brackets []
- *:
- Wildcards. // * match all
- @*:
- Wildcards. Matching all the properties // * [@ * = "hello"]
- Multi-criteria query
- and
- or
- //input[@id="kw" and @name="wd"]
Shortcuts browser F12 ----> Ctrl + F
Attribute name + node using labels positioned
- Syntax: // tag name [@ attribute name = value]
- //input[@id="kw" and @name="wd"]
Use text match : Function: text ()
- All matching text: text () = "text"
- // a [text () = "More"]
- Text partial match: contains (text (), the text part)
- (Translation: Kang ten dead)
- // a [contains (text (), "Product")]
By the attribute value matching section
- Syntax: // tag name [contains (@ attribute name, part of the property value)]
- //a[contains(@href,"om/more/") and @class="bri"]
Level positioning:
- First determine the node's parent, looking for a child node
- //div[@id="u1"]//a[@name="tj_login"]
axis positioning xpath
Using the syntax:
Known element tag name :: @ axis name [@ attribute = value]
- ancestor :( translation: ang sei st child)
- All before the current node parent node
- parent :( translation: pan run t)
- The parent of the current node
- preceding :( translation: pan run t)
- All nodes before the current node
- Sibling-Preceding :(译: pu rui sei ding sei bo li
- Before the current node all of the peer nodes
- following:(译:fao lin)
- All the current node after node
- following-sibling:(译:fao lin sei bo li)
- All siblings after the current node
- child
- The current node's children
- self
- The current node itself
8. css positioning themselves get ------
The browser comes with css, xpath, js positioning mode
Second, how to use
# Import library from the Selenium Import webdriver # open the Google browser session is established. Start Chromedriver.exe open Chrome Driver = webdriver.Chrome () # access Baidu Home driver.get ( " http://www.baidu.com " ) driver.find_element_by_xpath ( ' // span [@ class = "text-Setting" ] ' ) # find a driver.find_elements_by_xpath ( ' // span [text () = "settings"] ' ) # found plurality
******* Please respect the original, as to reprint, please indicate the source: Reprinted from: https://www.cnblogs.com/shouhu/ , thank you! ! *******