skip to Main Content
<label class="btn btn-default btn-sm ng-pristine ng-valid" ng-model="radio.activePane" btn-radio="1">
    <i class="glyphicon glyphicon-th"></i>
    Datos
</label>

I’m trying to find this element and then click on it. However I can’t find it and I don’t know exactly why.

I have tried this code but I get NoSuchElementException. I would like to find it by the text if it is possible. The class and the rest of the tags may change.

driver.find_element(By.XPATH, "//label[contains(text(), 'Datos')]").click()

Any ideas of how can I find the element and click on it?

2

Answers


  1. Use either of the two below XPath expressions:
    //*[text()[contains(.,'Datos')]] or //label[contains(.,'Datos')]

    driver.find_element(By.XPATH, "//*[text()[contains(.,'Datos')]]").click()
    

    or:

    driver.find_element(By.XPATH, "//label[contains(.,'Datos')]").click()
    
    Login or Signup to reply.
  2. If you are getting NoSuchElementException you can try the explicitWait

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    wait = WebDriverWait(driver, 20)
    element = wait.until(EC.presence_of_element_located((By.XPATH, "//label[contains(text(), 'Datos')]")))
    element.click()
    

    OR

    element = wait.until(EC.presence_of_element_located((By.XPATH, "//*[normalize-space(),'Datos']")))
    element.click()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search