Selenium+Python 自动化 之八种元素定位方法

导入selenium中的webdirver包

from selenium import webdriver

1、id定位

driver.find_element_by_id("username").send_keys("admin")

2、name定位

driver.find_element_by_name("username").send_keys("admin")

3、class定位

driver.find_element_by_class_name("")

4、tag定位

driver.find_element_by_tag_name("input")

5、link定位

driver.find_element_by_link_text("忘记密码").click()

6、partial link 定位

driver.find_element_by_partial_link_text("忘记密(关键字)").click()

7、XPath 定位

绝对路径定位

获取绝对路径的方法:

第一步:按F12

第二步:Ctrl+Shift+C,点击要定位的元素

第三步:在代码区域右击,选择Copy-CopyXpath

driver.find_element_by_xpath("/html/body/div/div[2]/div[1]/div[3]/div/button").click()

8、css定位

driver.find_element_by_css_selector("").send_keys()
原文地址:https://www.cnblogs.com/wanglisen/p/14156802.html