【问题标题】:How to make Selenium click on this button?如何让 Selenium 点击这个按钮?
【发布时间】: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


【解决方案1】:

您希望等待元素可点击,然后尝试点击它: 我 进口时间 从硒导入网络驱动程序 从 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)

【讨论】:

  • 好的,现在看起来很有希望。当我将此添加到您的代码'driver.execute_script("arguments[0].scrollIntoView();", ContinueButton) ActionChains(driver).move_to_element(ContinueButton).perform() time.sleep(3) ActionChains(driver)。 move_to_element(ContinueButton).click() time.sleep(5)' 我可以看到元素是如何找到并悬停在上面的 - 但点击不会导致动作。两个观察: 1.我需要将鼠标指针放在浏览器中,否则找不到元素。 2.如果有弹窗(newsletter,cookie footer),需要手动关闭,否则报错。
  • 另一个更新:使用您的代码和 'driver.execute_script("arguments[0].click();", ContinueButton)' 而不是 'ActionChains(driver).move_to_element(ContinueButton).click( )' 点击执行!但是,现在我遇到了一个新问题,因为网站响应“您无权访问 http://...” 有什么建议可以解决这个问题吗?
【解决方案2】:

问题是您正在单击li 元素。

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

试试这个:

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

【讨论】:

  • 导致同样的错误:NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//li[@class='nextPage'] /a"}(会话信息:chrome=86.0.4240.198)
  • 我在本地测试了代码,没有发现这样的错误。我所做的一项更改是使用 ContinueButton.click() 删除 ActionChains
  • 没有变化。基本问题似乎是无法定位按钮。
  • 我可以在本地计算机上完美运行代码。我只对您的代码进行了 3 处更改,添加了我的 chrome 驱动程序的路径 driver = webdriver.Chrome(options=options,executable_path=r'C:\chromedriver.exe'),将 /a 添加到 XPATH,并用简单的 .click() 替换了 ActionChains
  • 不,也不起作用。这似乎是一个普遍的问题,因为我刚刚在其他网页上尝试了相同的脚本,得到了同样的错误。
猜你喜欢
  • 1970-01-01
  • 2014-10-12
  • 2020-02-24
  • 2019-02-23
  • 2020-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多