英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论