【发布时间】:2020-11-16 14:43:34
【问题描述】:
我正在尝试提取此网页上的所有文章,但我无法让 Selenium 单击页面末尾的“继续”按钮。 我尝试了很多不同的版本,但我只会发布一个,至少不会引发错误...:
import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
addr = 'https://www.armani.com/de/armanicom/giorgio-armani/f%C3%BCr-ihn/alle-kleidungsstucke'
options = webdriver.ChromeOptions()
options.add_argument("--enable-javascript")
driver = webdriver.Chrome(options=options)
driver.get(addr)
ContinueButton = driver.find_element_by_xpath("//li[@class='nextPage']")
# gives: No error, but also no effect
# ContinueButton = driver.find_element_by_xpath("/html/body/div[3]/main/section/div[2]/div[1]/ul/li[8]/a/span[2]")
# gives: NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[3]/main/section/div[2]/div[1]/ul/li[8]/a/span[2]"}
#ContinueButton = driver.find_element_by_css_selector(".nextPage > a:nth-child(1)")
# gives: NoSuchElementException: no such element: Unable to locate element:
ActionChains(driver).move_to_element(ContinueButton).click()
time.sleep(5)
Chrome 引擎是 v86,但我也尝试过(但失败)了 Firefox。
【问题讨论】:
-
您是否正在关闭第一个页面加载时出现的通讯对话框?
-
有一个 CookiePolicy 页脚可能会拦截您的点击。在点击下一步按钮之前尝试
driver.find_element_by_id('footer_tc_privacy_button').click()关闭页脚 -
我已手动关闭它(因为我还不知道如何在程序中执行此操作),但它没有帮助。
-
添加页脚点击,给出以下错误消息: NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"[id="footer_tc_privacy_button "]"}(会话信息:chrome=86.0.4240.198)
-
好的。仅单击下一步按钮显示错误。你最初的问题
标签: python selenium selenium-webdriver web-scraping webdriver