前提:需要定位的元素或属性必须唯一。
id,name,class_name,xpath,css,tag_name,link_text,partial_link_text
id定位:
driver.find_element(By.ID, "kw").send_keys("")
name定位:
driver.find_element(By.NAME, "wd").send_keys("")
link_text定位(链接文本):
driver.find_element(By.LINK_TEXT, "新闻").click()
partial_link_text定位(部分链接文本):
driver.find_element(By.PARTIAL_LINK_TEXT, "新闻").click()
xpath定位:
绝对路径:/
复制完整的xpath
/html/body/div[1]/div[2]/div[5]/div[1]/div/form/span[1]/input
相对路径://
按Ctrl+F键启用搜索
//input
1. 相对路径+索引定位
//form/span[1]/input
2. 相对路径+属性定位
//input[@autocomplete="off"]
3. 相对路径+通配符定位
//[@*="off]
//*[@id="kw"]
复制xpath经常会出错,不是万能的
4. 相对路径+部分属性值定位
以开头://input[starts-with(@autocomplete,'of')]
//*[starts-with(@autocomplete,'of')]
以结尾://input[substring(@autocomplete,2)='ff']
包含://*[contains(@autocomplete,'ff')]
5. 相对路径+文本定位
//span[text()='按图片搜索']
CSS定位:不常用