寻找标签元素并点击它

huangapple go评论94阅读模式
英文:

Finding a label element and clicking on it

问题

  1. 我正在尝试找到这个元素然后点击它。然而,我找不到它,也不知道为什么。
  2. 我尝试了这段代码,但是我得到了NoSuchElementException。如果可能的话,我想通过文本找到它。类和其他标签可能会改变。
  3. `driver.find_element(By.XPATH, "//label[contains(text(), 'Datos')]").click()`
  4. 有什么办法可以找到这个元素并点击它吗?
英文:
  1. <label class="btn btn-default btn-sm ng-pristine ng-valid" ng-model="radio.activePane" btn-radio="1">
  2. <i class="glyphicon glyphicon-th"></i>
  3. Datos
  4. </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?

答案1

得分: 0

使用以下两个XPath表达式之一:

//*[text()[contains(.,'Datos')]]//label[contains(.,'Datos')]

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

或者:

  1. driver.find_element(By.XPATH, "//label[contains(.,'Datos')]").click()
英文:

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

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

or:

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

答案2

得分: 0

如果你遇到 NoSuchElementException 问题,你可以尝试使用 explicitWait

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

或者

  1. element = wait.until(EC.presence_of_element_located((By.XPATH, "//*[normalize-space(),'Datos']"))
  2. element.click()
英文:

If you are getting NoSuchElementException you can try the explicitWait

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

OR

  1. element = wait.until(EC.presence_of_element_located((By.XPATH, "//*[normalize-space(),'Datos']")))
  2. element.click()

huangapple
  • 本文由 发表于 2023年3月7日 18:04:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75660503.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定