1

I have been struggling a bit with locating elements in HTML - using selenium/python.

I have the following which identifies a button;

<button class="btn__primary--large from__button--floating" data-litms-control-urn="login-submit" type="submit" aria-label="Sign in">Sign in</button>

I have tried to click using;

driver.find_element_by_class_name('btn__primary--large from__button--floating').click()

However, I get the error message:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn__primary--large from__button--floating"}

Any general help in finding elements would be appreciated!

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
mikeowl
  • 11
  • 1

3 Answers3

1

To click on the Sign in element you can use either of the following Locator Strategies:

  • Using css_selector:

    driver.find_element_by_css_selector("button[data-litms-control-urn='login-submit'][aria-label='Sign in']").click()
    
  • Using xpath:

    driver.find_element_by_xpath("//button[@aria-label='Sign in' and text()='Sign in']").click()
    

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[data-litms-control-urn='login-submit'][aria-label='Sign in']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@aria-label='Sign in' and text()='Sign in']"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

There's a Space (" ") in your class name. You have to separate it with a dot (.) instead.

Instead of

driver.find_element_by_class_name('btn__primary--large from__button--floating').click()

Try:

driver.find_element_by_class_name('btn__primary--large.from__button--floating').click()
MendelG
  • 14,885
  • 4
  • 25
  • 52
0

You can find the button with text Sign in as well.

driver.find_element_by_xpath("//button[text()='Sign in']").click()
Arundeep Chohan
  • 9,779
  • 5
  • 15
  • 32