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!

0

    3 Answers 3

    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 
      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() 
      2
      • amazing, that worked! is that the case for all spaces in the html?
        – mikeowl
        CommentedDec 17, 2020 at 22:48
      • I think so, for all the class-names that have a space
        – MendelG
        CommentedDec 17, 2020 at 22:49
      0

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

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

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.