繁体   English   中英

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

[英]Cannot click on next button by using selenium

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

我想点击这个按钮(蓝色圆圈):在此处输入图片描述

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

这是我的代码:

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")

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM