Home > Net >  Selenium element selection issues and VSCode bug? Automate the Boring Stuff chapter 12
Selenium element selection issues and VSCode bug? Automate the Boring Stuff chapter 12

Time:02-02

I'm attempting to replicate this code:

from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://inventwithpython.com')

try:
    elem = browser.find_element_by_class_name(' cover-thumb')
    print('Found <%s> element with that class name!' % (elem.tag_name))
except:
    print('Was not able to find an element with that name.')

But it keeps returning the exception. I'm running this on mac w. vscode and there are few things off.

  1. find_element_by_class_name method doesn't seem to register as a method.
  2. Everytime I run this Intellicode prompts get disabled
  3. I can't run this at all on Chrome as it crashes the chrome browsers

I've also searched online for driver issues and have done webdriver with the chrome driver path. Didn't work either

This is the error I'm getting if run it without try and except.

elem = browser.find_element_by_class_name(' cover-thumb') ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'WebDriver' object has no attribute 'find_element_by_class_name'

CodePudding user response:

Selenium 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)
* ...

You now need to use:

driver.find_element("class name", "VALUE_OF_CLASS_NAME")

Eg:

driver.find_element("class name", "cover-thumb")

But if a class name has multiple segments, use the dot notation from a CSS Selector:

driver.find_element("css selector", "img.cover-thumb")
  • Related