我正在尝试用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)的主题,但是没有运气。
答案 0 :(得分: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
答案 1 :(得分:1)
根据您的问题单击下一个页面,直到到达最后一页,您可以使用以下解决方案:
代码块:
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
答案 2 :(得分:0)
有几件事需要照顾:
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()
答案 3 :(得分:0)
我遇到了同样的错误,解决方案不是将窗口滚动到对象(也许它可以修复一些错误,但对我来说不是)。 我的解决方案是使用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)