selenium之元素定位的方法

1、常见定位方式(id、name、class、tag、link、partial_link)

driver.find_element_by_id('id值')
driver.find_element_by_name('name值')
driver.find_element_by_class_name('class值')
driver.find_element_by_tag_name('标签名')
driver.find_element_by_link_text('文本链接')
driver.find_element_by_partial_link_text("很长的文本链接")

2、xpath定位

driver.find_element_by_xpath("/html/body/div/div[2]/div/div/div/from/span/input") #绝对定位
driver.find_element_by_xpath("//input[@id='kw']") #元素属性定位
driver.find_element_by_xpath("span[@class='bg s_ipt_wr']/input") #层级与属性结合
driver.find_element_by_xpath("input[@id='kw']and@class='su'/span/span") #逻辑运算符

3、css_selector定位

driver.find_element_by_css_selector("#id值")  #通过id属性定位
driver.find_element_by_css_selector(".class值") #通过class属性定位
driver.find_element_by_css_selector("input") #通过标签名定位
driver.find_element_by_css_selector("a[data-app=yxlz]") #通过属性定位
driver.find_element_by_css_selector(".user>input") #通过父与子层级定位
driver.find_element_by_css_selector(".selectCom dl:nth-child(2) h4") #dl是标签名,nth-child(2)就是dl下第2个层级下的h4标签;

4、通过TAB键,Enter键来定位

5、通过JS定位

upload = "openPopUpLayer(this,1);"  
driver.execute_script(upload)
 

猜你喜欢

转载自blog.csdn.net/endorstart/article/details/81222828