[英]How to make Selenium click on this button?
我試圖提取此網頁上的所有文章,但我無法讓 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。
您想等待元素可點擊,然后嘗試點擊它:I 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 = 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)
問題是您正在單擊li
元素。
收到您的點擊但未執行任何操作,為此您需要將a
元素定位在li
。
嘗試這個:
ContinueButton = driver.find_element_by_xpath("//li[@class='nextPage']/a")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.