You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何我的Selenium WebDriver没有find_element_by_xxx系列方法?

问题解答

这和ChromeDriver版本无关,是因为你用的是Selenium 4.x版本——这个版本移除了find_element_by_link_text、find_element_by_id等一系列以find_element_by_开头的旧方法,统一只保留find_element()和find_elements()两个基础方法。

要实现原find_element_by_link_text的功能,需要配合By类指定定位策略,步骤如下:

  1. 先导入By类:
from selenium.webdriver.common.by import By
  1. 调用find_element()方法时传入对应参数:
driver.find_element(By.LINK_TEXT, "你的链接文本")

如果是模糊匹配链接文本,用By.PARTIAL_LINK_TEXT即可:

driver.find_element(By.PARTIAL_LINK_TEXT, "部分链接文本")

同理,其他旧的定位方法也都可以用类似方式替换,比如:

  • 原find_element_by_id("xxx") → find_element(By.ID, "xxx")
  • 原find_element_by_xpath("xxx") → find_element(By.XPATH, "xxx")

内容的提问来源于stack exchange,提问作者blackscratch22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 01:27:15