0

My HTML format is below:

***button class="btn authorize unlocked"

span>Authenticate</span

button***

So when I use "browser.find_element_by_xpath('//span[@class = "btn authorize unlocked"]')" to locate this button, it can not find that.

Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@class = "btn authorize unlocked"]"} 

So what should I change?

1 Answer 1

1

To locate the element you can use either of the following Locator Strategies:

  • Using css_selector:

    element = driver.find_element(By.CSS_SELECTOR, "button.btn.authorize.unlocked > span")
    
  • Using xpath:

    element = driver.find_element(By.XPATH, "//button[@class='btn authorize unlocked']/span[text()='Authenticate']")
    

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

  • Using CSS_SELECTOR:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "button.btn.authorize.unlocked > span")))
    
  • Using XPATH:

    element = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//button[@class='btn authorize unlocked']/span[text()='Authenticate']")))
    
  • 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
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.