python - 如何使用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。有什么奇怪的事情阻止我直接点击这个按钮吗?
解决方案
如果你有异步加载,等到元素加载并准备好交互,使用内置的东西。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()
推荐阅读
- c# - 无法绑定 ViewModelLocator
- hibernate - java.lang.IllegalArgumentException:WFLYWELD0037:将持久性单元注入 CDI 托管 bean 时出错
- javascript - 每次页面加载时如何对端点进行网络调用
- ios - 带有redirectURI的Swift OAuth2.0
- bash - bash 中没有 sudo 的命令不起作用
- java - 如何在循环中更新标签
- ubuntu - 使用来自 Cloudflare 的 .pem 和 .key 设置 Ubuntu Apache2 SSL
- python - 使用字典和贪心算法解决 Set Cover 问题时的返回键
- java - 为什么 equals() 方法不能按预期工作?
- javascript - 如何在 2020 年使用纯 javascript 从 id 获取 YouTube 嵌入视频标题?