元素定位:
1、id定位:利用元素id属性定位
2、name定位:利用元素的name属性定位
3、class定位:利用class属性定位
当class属性值有空格时,空格必须使用点代替
4、tag定位:利用元素标签定位
5、link_text定位:利用超链接上的文本定位超链接
6、partial_link_text定位:利用超链接上的部分文本定位超链接
7、css定位:利用元素的层叠样式信息定位
在css定位中,使用class属性值定位元素时,如果class属性值中有空格,空格必须使用点代替。
8、xpath定位:元素相对路径定位方式
在xpath定位中,使用class属性值定位元素时, 如果class属性值中有空格,空格不能使用点代替。
在xpath中可以使用元素的文本定位
在xpath中可以支持contains和starts-with关键字
在xpath中使用父子兄弟节点定位:
父节点:parent 子节点:descendant 兄节点:preceding 弟节点:following
单元素定位:
find_element_by_XXX() 或 find_element(By.XXX, ‘XXX’)
返回值为定位到的元素对象
多元素定位:
find_elements_by_XXX() 或 find_elements(By.XXX, ‘XXX’)
返回值为一个列表,列表中为定位到的元素对象,
列表中元素的索引按照定位到的顺序从零开始依次递增。
定位不到元素,返回空列表
css定位 find_element(By.CSS_SELECTOR,’[class=“xxxx”]id=“xxxx”][name=“xxxx”]’)
find_element(By.CSS_SELECTOR,’.class属性值’)
find_element(By.CSS_SELECTOR,’#id属性值’)
xpath定位:find_element(By.XPATH,’//button[@id=“button_send”]’)
find_element(By.XPATH,’//[@id=“button_send”]’)
find_element(By.XPATH,’//[@id=“button_send” and @class=“xxxx”]’)
find_element(By.XPATH,’//button[text()=“立即发送”]’)
find_element(By.XPATH,’//button[contains(@id,“send”)]’)
find_element(By.XPATH,’//button[contains(text(),“发送”)]’)