如何让 Selenium 点击这个按钮?

How to make Selenium click on this button?

我正在尝试提取此网页上的所有文章,但我无法让 Selenium 单击页面末尾的“继续”按钮。 我已经尝试了很多不同的版本,但我只会 post 那个至少不会引发错误的版本...:[=​​11=]

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。

问题是您点击的是 li 元素。

收到您的点击但未执行任何操作,为此您需要在 li 之后定位 a 元素。

试试这个:

ContinueButton = driver.find_element_by_xpath("//li[@class='nextPage']/a")

您想等待元素可点击,然后尝试点击它: 我 导入时间 从 selenium 导入 webdriver 来自 selenium.webdriver.common.action_chains 导入 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 = WebDriverWait(driver, 20).until(
EC.element_to_be_clickable((By.XPATH, "//li[@class='nextPage']/a")))

ActionChains(driver).move_to_element(ContinueButton).click()
time.sleep(5)