The positioning python selenium (XPath)

The world's most remote distance, that you see elements, but can not find it.

This introduction xpath usage, id, class, name, tagname, etc. Other conventional usage is not introduced. But there is a lesson to be learned following the positioning element Dafa, as little as possible to do with xpath location, unless absolutely necessary, otherwise you'll understand why the flowers so red, -, -

1, the basic grammar

You can refer to the official online face

expression

description

nodename Select all the child nodes of this node
/ Choose from the root node
// Select the document from the current node in the node matching options, regardless of location
. The current node
.. The parent of the current node
@

Select Properties

Some special usage

expression description
* Matches any element node
@*

Matches any attribute node

  

2, xpath positioned to Baidu, for example

 

By positioning --- property, label @ [@ attribute = value]

xpath = '// input [@ id = "kw"]', Similarly, name is, '// input [@ name = " wd"]', class, type, etc. may be used including such methods

from Selenium Import the webdriver 
Driver = webdriver.Chrome () 
driver.get ( ' https://www.baidu.com/ ' ) 
XPath = ' // input [@ ID = "kW"] ' 
## represents the input tag input , the id attribute of the @ represents the seek, the id of kW value
driver.find_element_by_xpath (XPath) .send_keys ( ' Test ' )

--- By @text () positioning

 xpath = '// tag name [@text () = "string"]'

There is no good find chestnuts

General usage

xpath = '//*[@text()="text"]'

xpath = '//*[contains(@text(),"text")]'

 

--- fuzzy positioning using contains () is positioned

contains positioning is a very powerful way of positioning, as to why this mad boast method, such as when you use appium know, described later. Benpian below for a few chestnuts

xpath = "// tag name [contains (@ attribute, 'attribute')]"

from Selenium Import the webdriver
 Import Time 
Driver = webdriver.Chrome () 
driver.get ( ' https://www.baidu.com/ ' )
 # # id blurred by positioning, * represents any element from the matching node, of course, also possible to use other positioning properties, e.g. class @, @ name 
XPath = ' // * [the contains (@id, "kW")] ' 
driver.find_element_by_xpath (XPath) .send_keys ( ' kW ' )

----level position, that is, if an element has no attributes can locate it, then you can go look up its father, navigate to it according to hierarchy, if its father is consequently not, it went grandfather, or to take Baidu for example, pay attention not often see the elements of a map of Baidu - -!

xpath = '// tag fathers / grandfather tag / tag father / his tag'

from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
## 通过层级关系定位
xpath = '//form[@id="form"]/span/input'
driver.find_element_by_xpath(xpath).send_keys('kw')

--- positioning logic operation, which is the first one way, plus and, or so on, I basically have not seen, so there is also not recorded, are interested can look at Baidu own.

As xpath simplest, can also be directly f12, right-click copy xpath, but it looks like you could copy out a big lump of something, too long things look good na. Or a word, to practice, Practice makes perfect, you will succeed. Do not forget that time inspired teacher. - -!

 

Guess you like

Origin www.cnblogs.com/dflblog/p/11426062.html