selenium3 + python automation -xpath learning summary

First, the environment

  Browser: Chrome 77.0.3865.75

  selenium version: pip show selenium command cmd display 3.141.0

Two, xpath summary

  1.xpath + id / name / class Location:

  driver.find_element_by_xpath ( '// tag name [@ id = "id value"]')

  driver.find_element_by_xpath ( '// tag name [@ name = "name value"]')

  driver.find_element_by_xpath ( '// tag name [@ class = "class value"]')

  If you want to specify a label, the label name directly write specific tag name, if we are not specified, use *

  Example:

Driver.find_element_by_xpath # ( ' // * [@ ID = "kW"] ' ) .send_keys ( ' Python ' ) # * in place of any label 
# driver.find_element_by_xpath ( ' // INPUT [@ ID = "kW"] ' ) .send_keys ( ' Python ' ) specific labeling #

  2.xpath + other attributes Orientation:

  driver.find_element_by_xpath ( '// tag name [@ attribute name = "attribute value"]')

  3.xapth level positioning

  If xpath not directly targeting, you can first find its father or grandfather, and then layer by layer positioning

 

 

   Use it to locate the father, being given

Location # His father input box to locate the input, error result 
# driver.find_element_by_xpath ( '// span [@ class = "BG s_ipt_wr quickdelete-wrap"] / input'). Send_keys ( 'Python') 
# positioning his grandfather input box to locate the input 
# driver.find_element_by_xpath ( '// * [@ id = "form"] / span / input'). send_keys ( 'python')

  4.xpath index, xpath same label, positioning sibling elements, uses an index, where the index is calculated starting from 1, and different from python

#xpath索引,兄弟标签的定位,索引是从1开始计算,跟python的索引不同
text1=driver.find_element_by_xpath('//ul[@class="custom_dot  para-list list-paddingleft-1"]/li[1]/div').text
text2=driver.find_element_by_xpath('//ul[@class="custom_dot  para-list list-paddingleft-1"]/li[2]/div').text
text3=driver.find_element_by_xpath('//ul[@class="custom_dot  para-list list-paddingleft-1"]/li[3]/div').text
print(text1,text2,text3)

  5.xpath 逻辑计算 and/or/not,多个属性进行查找

  driver.find_element_by_xpath('//标签名[@属性1=“属性值1” and @属性2=“属性值2”]')

#xpath多属性
text=driver.find_element_by_xpath('//div[@class="para" and @label-module="para"]')
print(text)

   6.xpath模糊匹配,contains()用的多

  driver.find_element_by_xpath('//标签名[contains(text(),"要匹配的值")]')

  driver.find_element_by_xpath('//标签名[contains(@属性名,"要匹配的值")]')

  driver.find_element_by_xpath('//标签名[starts-with(@属性名,"要匹配的值")]')

#driver.find_element_by_xpath('//*[contains(text(),"hao123")]').click()
#xpath也可以模糊匹配某个属性
#driver.find_element_by_xpath('//*[contains(@id,"kw")]').send_keys('yangxuan')
#xpath可以模糊匹配以什么开头
driver.find_element_by_xpath('//*[starts-with(@id,"kw")]').send_keys('yx')

参考博客:https://www.cnblogs.com/yoyoketang/p/6123938.html

Guess you like

Origin www.cnblogs.com/xiaobeibi/p/12188039.html
Recommended