python - Python:元素不是可点击的硒
问题描述
我正在尝试用 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&sort=votes&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),但没有运气。
解决方案
根据您的问题单击下一页直到到达最后一页,您可以使用以下解决方案:
代码块:
from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support import expected_conditions as EC from selenium.common.exceptions import TimeoutException from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import StaleElementReferenceException options = webdriver.ChromeOptions() options.add_argument("start-maximized") options.add_argument('disable-infobars') driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe') driver.get("https://stackoverflow.com/users/37181/alex-gaynor?tab=tags") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='grid--cell fc-white js-notice-close' and @aria-label='notice-dismiss']"))).click() while True: try: driver.execute_script(("window.scrollTo(0, document.body.scrollHeight)")) WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='pager fr']//a[last()]/span[@class='page-numbers next']"))) driver.find_element_by_xpath("//div[@class='pager fr']//a[last()]/span[@class='page-numbers next']").click() except (TimeoutException, NoSuchElementException, StaleElementReferenceException) : print("Last page reached") break driver.quit()
控制台输出:
Last page reached
推荐阅读
- python - Parse column of nested JSON as pandas DataFrame
- angular - 无法在离子 3 中的地图上显示多个标记
- apache-kafka - 为了支持 18 台 kafka 机器,我们需要多少个 zookeper 服务器
- python-3.x - 有循环中断列表框?
- angular - 概括休息服务调用并将服务器响应映射到我的对象
- python - 在 Ansbile Vault 中存储密码并使用 API 从 Python 脚本中检索该密钥
- iis - 在 ASP.NET 中使用 QUERY_STRING 重写特定条件的 URL
- r - 在文本文件中的特定行之间粘贴文本?(右)
- r - 在 R 中重新组织数据框
- java - 如何在 JAVA 中将字符串变量转换回字节 []