Home > Enterprise >  AttributeError: 'WebDriver' object has no attribute 'find_element_by_class_name'
AttributeError: 'WebDriver' object has no attribute 'find_element_by_class_name'

Time:06-30

I want to extract price of the this machine. https://www.amazon.com/dp/B01N7GO468/ref=syn_sd_onsite_desktop_217?psc=1&spLa=ZW5jcnlwdGVkUXVhbGlmaWVyPUEyOTE3T1VVUk5UOVBXJmVuY3J5cHRlZElkPUEwNjg4MzM2MkZJUEJXTU1NT1FBQiZlbmNyeXB0ZWRBZElkPUEwMjQ2NzE1MUVQNEU3Tkk5VjJROSZ3aWRnZXROYW1lPXNkX29uc2l0ZV9kZXNrdG9wJmFjdGlvbj1jbGlja1JlZGlyZWN0JmRvTm90TG9nQ2xpY2s9dHJ1ZQ==. When I try to extract the price through selenium then it gives me error: AttributeError: 'WebDriver' object has no attribute 'find_element_by_class_name'.

CodePudding user response:

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:

from selenium.webdriver.common.by import By

driver.find_element(By.CLASS_NAME, THE_CLASS_NAME)

Or without the extra import:

driver.find_element("class name", THE_CLASS_NAME)

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

  • Related