寻找标签元素并点击它

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

Finding a label element and clicking on it

问题

我正在尝试找到这个元素然后点击它。然而,我找不到它,也不知道为什么。

我尝试了这段代码,但是我得到了NoSuchElementException。如果可能的话,我想通过文本找到它。类和其他标签可能会改变。

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

有什么办法可以找到这个元素并点击它吗?
英文:
<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?

答案1

得分: 0

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

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

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

或者:

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

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()

答案2

得分: 0

如果你遇到 NoSuchElementException 问题,你可以尝试使用 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()

或者

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

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()

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:

确定