python selenium ——— 动态id、class定位

什么样的是动态id呢?

动态id就是第一次点击显示的id与二次点击显示的不一样,一般是元素属性中包含一段数字的这种情况。

类似这种:

1  <input type="button" value="选择" id="btncussel1452">

这种情况,个人觉得比较简单的定位方法是用XPath来根据部分元素属性定位即模糊定位:

有三种情况:

 1  driver.find_element_by_xpath("//input[starts-with(@id,'btncusse')]").click()

 2  driver.find_element_by_xpath("//input[contains(@id,'btncusse')]").click()

 3  driver.find_element_by_xpath("//input[ends-with(@id,'btncusse')]").click()

解释一下:

btncusse:这个是id中不变的部分字符串;

starts-with:是id中以不变的部分字符串开头;contains:是id中包含不变的部分字符串;ends-with:是id中以不变的部分字符串结尾;

[]中括号前面,前面的input,是根据本条代码的Html标签决定的 。

**所用的是python selenium与Chrome浏览器**

posted on 2018-03-07 14:42  Serein&QH  阅读(2120)  评论(0编辑  收藏  举报

导航