gpt4 book ai didi

Python:元素不是可点击的 Selenium

转载 作者:行者123 更新时间:2023-11-28 17:04:32 25 4
gpt4 key购买 nike

我正在尝试用 Python 编写一个程序,该程序单击到下一页,直到它到达最后一页。我关注了 Stackoverflow 上的一些旧帖子并编写了以下代码:

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException


driver = webdriver.Chrome(executable_path="/Users/yasirmuhammad/Downloads/chromedriver")
driver.get("https://stackoverflow.com/users/37181/alex-gaynor?tab=tags")



while True:
try:
driver.find_element_by_link_text('next').click()

except NoSuchElementException:
break

但是,当我运行该程序时,它会抛出以下错误:

selenium.common.exceptions.WebDriverException: Message: unknown error: Element <a href="/users/37181/alex-gaynor?tab=tags&amp;sort=votes&amp;page=3" rel="next" title="go to page 3">...</a> is not clickable at point (1180, 566). Other element would receive the click: <html class="">...</html>
(Session info: chrome=68.0.3440.106)

我也关注了 Stackoverflow 的一个话题(selenium exception: Element is not clickable at point),但没有成功。

最佳答案

您需要先关闭此横幅 -

由于 selenium 打开了一个新的浏览器实例,因此网站会在您每次运行脚本时要求您存储 cookie。正是这个横幅出现在 Selenium 点击“下一步”按钮的方式中。使用此代码删除该关闭按钮 -

driver.find_element_by_xpath("//a[@class='grid--cell fc-white js-notice-close']").click()

此外,driver.find_element_by_link_text('next') 将抛出 StaleElementReferenceException。请改用此定位器 -

driver.find_element_by_xpath("//span[contains(text(),'next')]").click()

最终代码-

driver.get("https://stackoverflow.com/users/37181/alex-gaynor?tab=tags")

driver.find_element_by_xpath("//a[@class='grid--cell fc-white js-notice-close']").click()


while True:
try:
time.sleep(3)
driver.find_element_by_xpath("//span[contains(text(),'next')]").click()

except NoSuchElementException:
break

关于Python:元素不是可点击的 Selenium ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51928064/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com