webdriervAPI(XPath元素定位)

from  selenium  import  webdriver

driver  =  webdriver.Chorme()

driver.get("http://www.baidu.com")

"""XPath是一种在XML文档中定位元素的语言"""

绝对路径定位

driver.find_element_by_xpath(" ")  不推荐使用

利用元素属性定位,

  用户XPath不局限与id、name、和class三个属性值,元素的任意属性值都可以使用,只要它能唯一的标识一个元素

扫描二维码关注公众号,回复: 7517768 查看本文章

driver.find_element_by_xpath(" //input[@id='kw'] ")  指定标签名

driver.find_element_by_xpath(" //*[@id=' kw '] ")  不指定标签名

driver.find_element_by_xpath(" //input[@type=' submit '] ")  任意属性值

层级与属性结合

driver.find_element_by_xpath(" //div[@id= ' form ' ]/span[2]/input ")

使用逻辑运算符 "and"

    我们可以用and连接更多的属性来唯一地标识一个元素

driver.find_element_by_xpath(" //input[@id= ' kw ' and  @class=' su ' ]/span/input")

猜你喜欢

转载自www.cnblogs.com/97xiaolai/p/11706956.html