【问题标题】:Cannot click on next button by using selenium无法使用硒单击下一步按钮
【发布时间】:2021-05-08 03:32:39
【问题描述】:

我正在尝试使用 Selenium 来获取此链接的所有评论。但是,我的代码总是返回错误,我无法单击下一步按钮查看下一页中的评论。 我使用 CSS_SELECTOR 和 XPATH 尝试了一些,但仍然无法正常工作。

我想点击这个按钮(蓝色圆圈): enter image description here

我也尝试使用请求中的网络,但没有找到任何东西。 有人可以看看我能做什么。非常感谢。

这是我的代码:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
link = "https://www.conforama.fr/chambre-literie/textile-literie/couette/couette-240x220-cm-2-oreillers-60x60-cm-kit-couette-oreiller/p/473785#productInformations"

driver = webdriver.Chrome(executable_path=r'C:\Users\Desktop\python_export\chromedriver.exe')

driver.get(link)
time.sleep(3)

WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "bv_main_container_row_flex"))).click()
time.sleep(1)
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CLASS_NAME, "bv_main_container_row_flex"))).click()
time.sleep(2)


try:
    next_page = "button.bv-content-btn.bv-content-btn-pages.bv-content-btn-pages-last.bv-focusable.bv-content-btn-pages-active"
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, next_page))).click()
    print("Working at the first try")
except:
    print("Not working at the first try")
    try:
        next_page = "li.bv-content-pagination-buttons-item.bv-content-pagination-buttons-item-next"
        WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.CSS_SELECTOR, next_page))).click()
        print("Working at the second try")
    except:
        print("Not working at the second try")
        try:
            next_page = """//*[@id="BVRRContainer"]/div/div/div/div/div[3]/div/ul/li[2]/button"""
            WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, next_page))).click()
            print("Working at the third try")
        except:
            print("Not working at the third try")

【问题讨论】:

  • 你卡在哪一行了?
  • 嗨。我正在尝试使用不同的选项单击下一个按钮。你可以看到我尝试了 3 次尝试,除了看看哪个有效。但根本不起作用
  • 使用您尝试单击的按钮的基于文本的 HTML 更新问题。
  • 我已经更新了。
  • 可能由于地区限制,下一页箭头元素根本没有出现在我的系统中。

标签: python html selenium web web-crawler


【解决方案1】:

您需要先接受cookie。要单击 cookie 按钮,请使用以下代码,然后尝试使用您的第二次尝试代码单击 next 按钮。

#Accept cookie
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#_evidon-accept-button"))).click()

try:
   next_page = "li.bv-content-pagination-buttons-item.bv-content-pagination-buttons-item-next"
   WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, next_page))).click()
   print("Working at the first try")
except:
    print("Not working at the first try")

【讨论】:

  • @DaisyLily:太好了。如果答案解决了您的问题,请将其标记为已接受。检查how accept answer
猜你喜欢
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-05
  • 1970-01-01
  • 2017-12-17
相关资源
最近更新 更多