【问题标题】:Python: Element is not clickable seleniumPython:元素不是可点击的硒
【发布时间】:2019-01-26 10:12:14
【问题描述】:

我正在尝试用 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) 的一个线程,但没有运气。

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver webdriver


    【解决方案1】:

    您需要先关闭此横幅 -

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

    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
    

    【讨论】:

    • 感谢您的回答。但仍然收到以下错误:
    • selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attach to the page document
    • 谢谢。程序可以单击几下,但随后会引发以下错误:
    • selenium.common.exceptions.StaleElementReferenceException:消息:过时的元素引用:元素未附加到页面文档(会话信息:chrome=68.0.3440.106)
    • @user2293224 我的错,检查最终代码,您需要将 sleep 放在循环中,以便在新页面加载时等待一段时间。它在我身边工作。
    【解决方案2】:

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

    • 代码块:

      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
      

    【讨论】:

      【解决方案3】:

      有几点需要注意:

      1. 似乎该元素被 cookie 横幅隐藏了。通过滚动 可以使元素可用的页面。
      2. 当您单击 next - 页面已重新加载。所以你需要处理 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()
      

      【讨论】:

        【解决方案4】:

        我遇到了同样的错误,解决方案不是将窗口滚动到对象(也许它可以修复一些错误,但在我的情况下不是)。 我的解决方案是使用javascript,代码如下:

        click_goal = web.find_element_by_xpath('//*[@id="s_position_list"]/ul/li[1]/div[1]/div[1]/div[1]/a/h3')
        web.execute_script("arguments[0].click();", click_goal)
        

        【讨论】:

          猜你喜欢
          • 2019-11-28
          • 2018-02-04
          • 1970-01-01
          • 2016-07-08
          • 2015-01-12
          • 2016-05-17
          • 1970-01-01
          • 1970-01-01
          • 2019-05-30
          相关资源
          最近更新 更多