首页 > 解决方案 > 如何使用python selenium递归单击按钮

问题描述

我想递归地单击一个按钮以使用 python selenium 在目标上加载客户评论。例如,使用这个产品:
https
://www.target.com/p/vanity-fair-everyday-white-napkins-250ct/-/A-14739020 所以在客户评论页面的底部,有一个按钮来“加载更多 8 个”评论。我想通过打开产品页面在 selenium 中模拟这一点,滚动到页面底部以确保元素加载,然后在元素的 xpath 上单击(),如下所示:

from time import sleep
import csv
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import WebDriverException

class targetProduct:
    '''given a product page, return all of the customer reviews'''
    def __init__(self):
        #start chrome driver
        self.mydriver = webdriver.Chrome('/chromedriver.exe')
        #enter a product page URL with reviews
        self.productpage = "https://www.target.com/p/vanity-fair-everyday-white-napkins-250ct/-/A-14739020"

    def open_page(self):
        #open the product page and maximize the window
        self.mydriver.get(self.productpage)
        self.mydriver.maximize_window()
        sleep(5)

    def button_click(self):
        try:
            #scroll to the bottom of the page and sleep to ensure the button loads
            self.mydriver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
            sleep(3)
            #click on the button to load 8 more reviews
            self.mydriver.find_element_by_xpath('//*[@id="mainContainer"]/div/div/div[2]/div[5]/div/div[2]/div[3]/button').click()
            #sleep to load more reviews, then try clicking again to load addition reviews
            sleep(3)
            self.button_click()
        except NoSuchElementException:
            print("cant find more reviews button, continuing")

tp = targetProduct()
tp.open_page()
tp.button_click()  

看起来一切正常,除了程序无法正确找到“加载 8 条评论”按钮。我像往常一样直接从网站上复制了 xpath。有什么奇怪的事情阻止我直接点击这个按钮吗?

标签: pythonpython-3.xseleniumselenium-webdriverselenium-chromedriver

解决方案


如果你有异步加载,等到元素加载并准备好交互,使用内置的东西。Flattime.sleep(3)可能不够好,您的代码确实为我工作,但加载时间较慢可能会导致失败。 expected_conditions.presence_of_element_located允许您让您的按钮准备好被点击。

from time import sleep
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By


class targetProduct:
    '''given a product page, return all of the customer reviews'''
    def __init__(self):
        #start chrome driver
        self.mydriver = webdriver.Chrome('/chromedriver.exe')
        #enter a product page URL with reviews
        self.productpage = "https://www.target.com/p/vanity-fair-everyday-white-napkins-250ct/-/A-14739020"

    def wait_for_elem_by_xpath(self, xp):
        elem = WebDriverWait(self.mydriver, 8).until(EC.presence_of_element_located((By.XPATH, xp)))
        return elem

    def open_page(self):
        #open the product page and maximize the window
        self.mydriver.get(self.productpage)
        self.mydriver.maximize_window()
        sleep(5)

    def button_click(self):
        try:
            #scroll to the bottom of the page and sleep to ensure the button loads
            self.mydriver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

            #click on the button to load 8 more reviews
            more_button = self.wait_for_elem_by_xpath('//*[@data-test="load-more-btn"]/button')
            more_button.click()
            #sleep to load more reviews, then try clicking again to load addition reviews
            sleep(3)
            self.button_click()
        except TimeoutException:
            print("cant find more reviews button, continuing")

tp = targetProduct()
tp.open_page()
tp.button_click()

推荐阅读