1、常见的定位方式
id定位:find_element_by_id()
name定位:find_element_by_name()
class定位:find_element_by_class_name()
link定位:find_element_by_link_text()
partial link定位:find_element_by_partial_link_text()
tag定位:find_element_by_tag_name()
xpath定位:find_element_by_xpath()
css定位:find_element_by_css_selector()
2、xpath定位
2.1 普通的xpath定位
driver.find_element_by_xpath("xpath路径")
2.2 通过id的方式获取到的xpath
# 通过id='FormListId'进行定位 driver.find_element_by_xpath("//*[@id='FormListId']")
2.3 通过placeholder属性获取到的xpath
# 定位placeholder属性为"请搜索"的input元素,即<input placeholder="请搜索"> driver.find_element_by_xpath("//input[@placeholder='请搜索']")
2.4 通过id属性所在元素的下一个元素获取的xpath
# 定位id="formid"元素-下层元素span-下层元素第二个input driver.find_element_by_xpath("//*[@id='formid']/span/input[1]")
2.5 多条件定位xpath
# 多条件定位 driver.find_element_by_xpath("//input[@name='urd' and @value='psd']")
2.6 根据文本定位xpath
# 根据文本定位元素 driver.find_element_by_xpath("//li[text()="姓名:"]")
2.7 根据关键词定位xpath
# 根据关键字 driver.find_element_by_xpath("//a[contains(@href,"aaa")]")
2.8 文本包含 苹果 内容
#在a标签下有个文本(text)包含(contains)'苹果' 的元素 driver.find_element_by_xpath("//a[contains(text(),'苹果')]")
2.9 根据href定位元素xpath
#a的标签,链接href='http://www.baidu.com/ 的元素 driver.find_element_by_xpath("//a[@href='http://www.baidu.com/']")