python selenium xpath定位方式

https://blog.csdn.net/huiseqiutian/article/details/73739707

 

3.标签属性定位:

3.1标签属性定位,相对比较简单,也要求属性能够定位到唯一一个元素,如果存在多个相同条件的标签,默认只是第一个,具体格式

xxx.find_element_by_xpath("//标签[@属性==‘属性值’]")

属性判断条件:最常见为id,name,class等等,目前属性的类别没有特殊限制,只要能够唯一标识一个元素都是可以的

具体例子

xxx.find_element_by_xpath("//a[@href='/industryMall/hall/industryIndex.ht']")
xxx.find_element_by_xpath("//input[@value='确定']")
xxx.find_element_by_xpath("//div[@class = 'submit']/input")

当某个属性不足以唯一区别某一个元素时,也可以采取多个条件组合的方式,具体例子

xxx..find_element_by_xpath("//input[@type='name' and @name='kw1']")

 

3.2 当标签属性很少,不足以唯一区别元素时,但是标签中间中间存在唯一的文本值,也可以定位,其具体格式

xxx.find_element_by_xpath("//标签[contains(text(),'文本值')]")

 

具体例子:

 

xxx.find_element_by_xpath("//iunpt[contains(text(),'型号:')]")

注意:尽量在html中复制此段文本,避免因为肉眼无法分辨的字符导致定位失败

 

3.3 其他的属性值如果太长,也可以采取模糊方法定位,直接上示例

 

xxx.find_element_by_xpath(“//a[contains(@href, ‘logout’)]”)

 

3.4 XPath 关于网页中的动态属性的定位,例如,ASP.NET应用程序中动态生成id属性值,可以有以下四种方法:

a.starts-with 例子: input[starts-with(@id,'ctrl')] 解析:匹配以ctrl开始的属性值

b.ends-with 例子:input[ends-with(@id,'_userName')] 解析:匹配以userName结尾的属性值

c.contains() 例子:Input[contains(@id,'userName')] 解析:匹配含有userName属性值

posted @ 2021-12-14 12:04  小毛编  阅读(199)  评论(0编辑  收藏  举报