簡體   English   中英

Python:元素不是可點擊的硒

[英]Python: Element is not clickable selenium

我正在嘗試用 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 異常:Element is not clickable at point )但沒有運氣。

你需要先關閉這個橫幅 -

由於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

根據你的問題,通過接下來的頁面中點擊,直到它到達最后一頁,您可以采用如下方案:

  • 代碼塊:

     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 

有幾件事需要注意:

  1. 看起來這個元素被cookies橫幅隱藏了。 通過滾動頁面,可以使元素可用。
  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()

我遇到了同樣的錯誤,解決方案不是將窗口滾動到對象(也許它可以修復一些錯誤,但在我的情況下不能)。 我的解決方案是使用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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM