Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 无法单击该链接_Python 3.x_Selenium_Selenium Webdriver_Phantomjs_Wait - Fatal编程技术网

Python 3.x 无法单击该链接

Python 3.x 无法单击该链接,python-3.x,selenium,selenium-webdriver,phantomjs,wait,Python 3.x,Selenium,Selenium Webdriver,Phantomjs,Wait,我想转到第1页,然后单击指向第2页的其他按钮,然后单击第2页上的链接。 我的代码: from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC import selenium.webdriver.support.ui as UI d = webdriver.Firefox()

我想转到第1页,然后单击指向第2页的其他按钮,然后单击第2页上的链接。 我的代码:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import selenium.webdriver.support.ui as UI

d = webdriver.Firefox()
d.get("https://www.youtube.com/")
d.find_element_by_xpath('//*[@id="appbar-nav"]/ul/li[2]/a/span').click()
wait = UI.WebDriverWait(d, 20)
next_page_link = wait.until(
EC.element_to_be_clickable((By.LINK_TEXT, 'FACTS ABOUT ME')))
next_page_link.click()
我添加了wait,但它仍然给出了错误:

引发异常类(消息、屏幕、堆栈跟踪) selenium.common.exceptions.StaleElementReferenceException:Message:元素不再附加到DOM


如果一个元素根本不在DOM中,它将抛出一个NoTouchElementException。您是说您收到了StaleElementReferenceException。当web驱动程序搜索匹配的元素并找到一个元素时,会发生该异常。然后就有了对该元素的引用,但在单击该元素之前,它所绑定的元素将从DOM中删除。这通常是因为JavaScript库动态地更改了DOM,并从页面中删除了节点,然后将一个新节点放回原处。它看起来(视觉上)仍然在页面上,但驱动程序先前找到的元素实例不再存在。如果你在一个像这样使用JavaScript的网站上,唯一真正的处理方法是将位置和执行打包成一个循环,然后重试。

我找不到任何链接文本为“关于我的事实”的元素,我尝试了
通过css\u选择器(“a[title='FACTS ABOUT ME'])
,它对我有效。请尝试使用
find_by_css_selector
而不是
LINK_TEXT
?我的意思是做一个循环(有某种超时或n次,以防出错),查找元素(例如
driver.find_element_by_xpath
)并单击循环中的元素(也是在忽略StaleElementReference异常的try-catch块内部。)