'WebDriver' object has no attribute 'find_element_by_link_text' - Selenium script suddenly stopped working
Asked Answered
P

1

7

This is a weird issue I have ran into and I can't find any solution for this across the internet. I was using selenium in google colab to scrape a website and my code was working completely fine. I woke up the next day and ran the code again without changing a single line and don't know how/why my code starting giving me this error, AttributeError: 'WebDriver' object has no attribute 'find_element_by_link_text'. Same for find_element_by_class_name and id etc. I then rechecked a previously working script just to confirm and that gave me the same error too. I am confused about what happened suddenly and the scripts started giving me these errors.

How do I solve this? What am I doing wrong here?

!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()
Pudens answered 25/6, 2022 at 13:39 Comment(0)
S
25

Selenium just removed that method in version 4.3.0. See the CHANGES: 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

You now need to use:

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

For improved reliability, you should consider using WebDriverWait in combination with element_to_be_clickable.

Streptomycin answered 25/6, 2022 at 14:0 Comment(3)
Goodness! I knew this had something to do with version and checked selenium github yesterday but couldn't find my answer. Thanks a lot! Appreciate it.Pudens
Here's the docs for find_element: selenium-python.readthedocs.io/locating-elements.htmlUniat
OP sure the new code is correct? I think it would need to be driver.find_element(By.LINK_TEXT, 'Pet. Eng. Handbook')Broadcaster

© 2022 - 2024 — McMap. All rights reserved.