使用 selenium 无法点击下一步按钮

时间:2021-02-03 20:42:15

标签: python html selenium web web-crawler

我正在尝试使用 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")

1 个答案:

答案 0 :(得分:0)

您需要先接受 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")