mikeowl
mikeowl

Reputation: 11

Finding element to click in html with Selenium and Python

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!

Upvotes: 1

Views: 1715

Answers (3)

undetected Selenium
undetected Selenium

Reputation: 193098

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
    

Upvotes: 1

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

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

driver.find_element_by_xpath("//button[text()='Sign in']").click()

Upvotes: 0

MendelG
MendelG

Reputation: 20018

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()

Upvotes: 0

Related Questions