- 通过 ID 定位(find_element(By.ID, "id"))
- 最常用、最高效的方式。
- HTML 示例:<input id="username" type="text">
- 代码示例:
driver.find_element(By.ID, "username")
- 通过 Name 定位(find_element(By.NAME, "name"))
- 使用元素的 name 属性。
- HTML 示例:<input name="password" type="password">
- 代码示例:
driver.find_element(By.NAME, "password")
- 通过 Class Name 定位(find_element(By.CLASS_NAME, "class"))
- 使用元素的 class 属性(注意:只支持单个 class 名,不能包含空格)。
- HTML 示例:<button class="btn-primary">Submit</button>
- 代码示例:
driver.find_element(By.CLASS_NAME, "btn-primary")
- 通过 Tag Name 定位(find_element(By.TAG_NAME, "tag"))
- 通过 HTML 标签名定位(如 div, input, a 等)。
- 代码示例:
driver.find_element(By.TAG_NAME, "a")
- 通过 Link Text 定位(find_element(By.LINK_TEXT, "text"))
- 专门用于定位 <a> 标签(超链接),匹配完整可见文本。
- HTML 示例:<a href="/home>Home Page</a>
- 代码示例:
driver.find_element(By.LINK_TEXT, "Home Page")
- 通过 Partial Link Text 定位(find_element(By.PARTIAL_LINK_TEXT, "partial_text"))
- 匹配 <a> 标签中部分可见文本。
- 适用于动态或长文本链接。
- 代码示例:
driver.find_element(By.PARTIAL_LINK_TEXT, "Home")
- 通过 CSS Selector 定位(find_element(By.CSS_SELECTOR, "selector"))
- 强大且灵活,支持复杂选择器(如属性、层级、伪类等)。
- HTML 示例:<input data-test="email">
- 代码示例:
driver.find_element(By.CSS_SELECTOR, "input[data-test='email']")
- 通过 XPath 定位(find_element(By.XPATH, "xpath"))
- 功能最强大,可基于路径、属性、文本、位置等进行精确定位。
- 支持绝对路径和相对路径。
- 完整代码示例:
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
# 启动浏览器(以 Chrome 为例)
driver = webdriver.Chrome()
try:
# 打开一个测试页面(你可以替换成你自己的网页)
driver.get("https://www.baidu.com")
time.sleep(2) # 等待页面加载
# 1. 相对路径 + 索引定位: //form/span[1]/input
try:
element1 = driver.find_element(By.XPATH, "//form/span[1]/input")
print("✅ 成功通过索引定位到元素:", element1.tag_name)
except Exception as e:
print("❌ 索引定位失败:", e)
# 2. 相对路径 + 属性定位: //input[@autocomplete='off']
try:
element2 = driver.find_element(By.XPATH, "//input[@autocomplete='off']")
print("✅ 成功通过属性定位到元素:", element2.get_attribute("name"))
except Exception as e:
print("❌ 属性定位失败:", e)
# 3. 相对路径 + 通配符定位: //*[@autocomplete='off']
try:
element3 = driver.find_element(By.XPATH, "//*[@autocomplete='off']")
print("✅ 成功通过通配符定位到元素:", element3.tag_name)
except Exception as e:
print("❌ 通配符定位失败:", e)
# 4. 相对路径 + 部分属性值定位
# 以开头: starts-with
try:
element4a = driver.find_element(By.XPATH, "//*[starts-with(@autocomplete,'of')]")
print("✅ 成功通过 starts-with 定位:", element4a.tag_name)
except Exception as e:
print("❌ starts-with 定位失败:", e)
# 包含: contains
try:
element4b = driver.find_element(By.XPATH, "//*[contains(@autocomplete,'of')]")
print("✅ 成功通过 contains 定位:", element4b.tag_name)
except Exception as e:
print("❌ contains 定位失败:", e)
# 注意:substring(@attr, start_index) 在 XPath 1.0 中索引从 1 开始
# substring(@autocomplete,2)='ff' 表示从第2个字符开始截取等于 'ff'
try:
element4c = driver.find_element(By.XPATH, "//*[substring(@autocomplete,2)='ff']")
print("✅ 成功通过 substring 定位:", element4c.tag_name)
except Exception as e:
print("❌ substring 定位失败:", e)
# 5. 相对路径 + 文本定位
# 百度首页有“按图片搜索”按钮(可能在某些地区/设备不显示,这里用“百度一下”代替更稳妥)
try:
# 尝试定位“百度一下”按钮(文本为“百度一下”)
element5 = driver.find_element(By.XPATH, "//input[@value='百度一下']")
print("✅ 成功通过文本(value属性)定位到按钮")
except Exception as e:
print("⚠️ 未找到‘百度一下’按钮,尝试其他文本...")
# 如果你想严格按 text() 定位(适用于 <span>、<a> 等标签内的文本)
# 例如://span[text()='按图片搜索']
# 这里我们模拟一个包含该文本的页面(或你可以替换为实际页面)
# 百度首页可能没有“按图片搜索”,所以此操作可能失败,仅作语法演示
try:
element5_text = driver.find_element(By.XPATH, "//span[text()='按图片搜索']")
print("✅ 成功通过 text() 定位到 span 元素")
except Exception as e:
print("ℹ️ 未找到文本为‘按图片搜索’的 span(正常,因页面可能无此元素)")
finally:
# 关闭浏览器
time.sleep(2)
driver.quit()