为何我的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类指定定位策略,步骤如下:
- 先导入
By类:
from selenium.webdriver.common.by import By
- 调用
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
相关产品推荐
相关产品推荐

