[英]Selenium WebDriver find_element_by_xpath not working for text
I'm trying to click on a link on a webpage that has no ID and no individual class.我正在尝试单击没有 ID 和单个类的网页上的链接。 The only thing to lock it down to is the text 'Sessions'.
唯一可以锁定它的是文本“会话”。
I have tried:我试过了:
driver.find_element_by_xpath("//*[contains(text(),'Sessions')]");
driver.find_element_by_xpath("//*[text()='Sessions']");
Both come back with "No such element".两者都返回“没有这样的元素”。
Edit: I have also tried driver.find_element_by_link_text
which also didn't work.编辑:我也试过
driver.find_element_by_link_text
也没有用。
I've tried using the full xpath:我试过使用完整的 xpath:
/html/body/div/div/div[1]/div/nav/a[3]
To no avail.无济于事。
That is a link_Text
cause it's between anchor tag, use this :那是一个
link_Text
因为它在锚标记之间,使用这个:
driver.find_element_by_link_text('Sessions').click()
or或者
A way more good approach is to use ExplicitWaits
:一种更好的方法是使用
ExplicitWaits
:
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.LINK_TEXT, 'Sessions')))
element.click()
If you want explicit wait you would need to import the below :如果您想要显式等待,则需要导入以下内容:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
If the above gives you NoSuchElementException
, I would probably suspect this it is in iframe (See the screenshot first tag - I can see body), if it happens to be then in that case you would need to switch to iframe first and continute with this web element.如果上面给你
NoSuchElementException
,我可能会怀疑它在 iframe 中(请参阅屏幕截图第一个标签 - 我可以看到正文),如果恰好是这样,那么在这种情况下,您需要先切换到 iframe 并继续使用此网络元素。
Code代码
wait = WebDriverWait(driver, 10)
wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, "iframe xpath here")))
wait.until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "Sessions"))).click()
Imports :进口:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.