【问题标题】:'WebDriver' object has no attribute 'find_element_by_link_text' - Selenium script suddenly stopped working\'WebDriver\' 对象没有属性 \'find_element_by_link_text\' - Selenium 脚本突然停止工作
【发布时间】:2023-01-27 00:43:10
【问题描述】:

这是我遇到的一个奇怪的问题,我在互联网上找不到任何解决方案。我在 google colab 中使用 selenium 来抓取网站,我的代码工作得很好。第二天我醒来并再次运行代码而没有更改一行并且不知道我的代码如何/为什么开始给我这个错误,AttributeError:'WebDriver'对象没有属性'find_element_by_link_text'。对于 find_element_by_class_name 和 id 等也是如此。然后我重新检查了一个以前工作的脚本只是为了确认,这也给了我同样的错误。我对突然发生的事情感到困惑,脚本开始给我这些错误。

我该如何解决这个问题?我在这里做错了什么?

!pip install selenium
!apt-get update 
!apt install chromium-chromedriver

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',options=chrome_options)

driver.get("https://petrowiki.spe.org/PetroWiki")
driver.title #this line is returning the correct title value, code is able to access the url

peh = driver.find_element_by_link_text('Pet. Eng. Handbook')
peh.click()

【问题讨论】:

    标签: python selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    Selenium 刚刚在版本4.3.0 中删除了该方法。查看更改:https://github.com/SeleniumHQ/selenium/blob/a4995e2c096239b42c373f26498a6c9bb4f2b3e7/py/CHANGES

    Selenium 4.3.0
    * Deprecated find_element_by_* and find_elements_by_* are now removed (#10712)
    * Deprecated Opera support has been removed (#10630)
    * Fully upgraded from python 2x to 3.7 syntax and features (#10647)
    * Added a devtools version fallback mechanism to look for an older version when mismatch occurs (#10749)
    * Better support for co-operative multi inheritance by utilising super() throughout
    * Improved type hints throughout
    

    您现在需要使用:

    driver.find_element("link text", "Pet. Eng. Handbook")
    

    为了提高可靠性,您应该考虑结合使用 WebDriverWaitelement_to_be_clickable

    【讨论】:

    猜你喜欢
    • 2015-03-27
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 2022-01-18
    • 1970-01-01
    • 2012-06-28
    • 2012-10-06
    • 2022-01-27
    相关资源
    最近更新 更多