Python:元素不可单击

Python:元素不可单击,python,python-3.x,selenium,selenium-webdriver,webdriver,Python,Python 3.x,Selenium,Selenium Webdriver,Webdriver,我正在尝试用Python编写一个程序,它可以单击下一页,直到到达最后一页。我遵循了Stackoverflow的一些老帖子,并编写了以下代码: from selenium import webdriver from selenium.common.exceptions import NoSuchElementException driver = webdriver.Chrome(executable_path="/Users/yasirmuhammad/Downloads/chromedriv

我正在尝试用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)
selenium.common.exceptions.WebDriverException:消息:未知错误:元素在点(1180566)处不可单击。其他元素将收到单击:。。。
(会话信息:chrome=68.0.3440.106)

我也遵循了Stackoverflow()的线索,但运气不好。

您需要先关闭此横幅-

因为selenium会打开一个新的浏览器实例,所以每次运行脚本时,网站都会要求您存储cookie。正是这条横幅阻碍了selenium点击“下一步”按钮。使用此代码删除关闭按钮-

driver.find_element_by_xpath("//a[@class='grid--cell fc-white js-notice-close']").click()
另外,
driver.find\u element\u by\u link\u 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

根据您的问题,单击下一页,直到到达最后一页,您可以使用以下解决方案:

  • 代码块:

    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
    

    • 有两件事需要注意:

    • 元素似乎被cookies横幅隐藏了。通过滚动 可以使用该元素的页面
    • 当你点击
      下一步
      -重新加载页面。所以你需要处理这个问题
      StaleElementException
    • 添加这两个选项后,代码如下所示:

      from selenium import webdriver
      from selenium.common.exceptions import NoSuchElementException
      from selenium.common.exceptions import StaleElementReferenceException
      
      driver = webdriver.Chrome()
      driver.get("https://stackoverflow.com/users/37181/alex-gaynor?tab=tags")
      driver.execute_script(("window.scrollTo(0, document.body.scrollHeight)"))
      
      while True:
          try:
              webdriver.ActionChains(driver).move_to_element(driver.find_element_by_link_text('next')).click().perform()
          except NoSuchElementException:
              break
          except StaleElementReferenceException:
              pass
      
      print "Reached the last page"
      driver.quit()
      

      谢谢你的回答。但是仍然会出现以下错误:selenium.common.exceptions.StaleElementReferenceException:Message:stale元素引用:元素未附加到页面文档谢谢。程序运行了几次点击,但随后抛出以下错误:selenium.common.exceptions.StaleElementReferenceException:Message:stale element reference:element未附加到页面文档(会话信息:chrome=68.0.3440.106)@user229324 My bad,检查最终代码,您需要将睡眠放在循环中,以便它在加载新页面时等待一段时间。它在我这边起作用。