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?

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Iory
  • 25
  • 6

1 Answers1

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352