Home > Software design >  python selenium 4.8.0 -
python selenium 4.8.0 -

Time:02-04

So i have a log-in script and i just try find element by class on newest version of selenium (4.8.0)

code:

button = browser.find_element(By.CLASS_NAME, 'just class name')

AND this code not working.

my stuff:

  • firefox driver - 0.32.1 (2023-02-02, b7f075124503)
  • selenium 4.8.0

this is how the documentation searches for elements - By.CLASS_NAME, By.ID, etc...

https://www.selenium.dev/documentation/webdriver/elements/finders/

tried other variations of the code like find_element_by_ID and etc, installed an additional library for the selenium (I do not remember the name).

CodePudding user response:

CSS classes and IDs never start with spaces, and they also never have spaces. Check if this clases exist in DOM by typing in brosware console:

document.querySelectorAll('.class_name')

change class_name with class name you looking for. If object have multiple classes they are separated by space.

CodePudding user response:

By.CLASS_NAME accepts only single classname as an argument. So you can't pass multiple classnames.


Solution

You can use either of the following locator strategies:

  • Using the classname just:

    button = browser.find_element(By.CLASS_NAME, 'just')
    
  • Using the classname class:

    button = browser.find_element(By.CLASS_NAME, 'class')
    
  • Using the classname name:

    button = browser.find_element(By.CLASS_NAME, 'name')
    

As an alternative, you can also use a css_selector as follows:

button = browser.find_element(By.CSS_SELECTOR, ".just.class.name")
  • Related