问:无法通过 python 中的 chorme web 驱动程序持续单击按钮

发布于 01-20 08:58 字数 1348 浏览 3 评论 0原文

问题

如何通过点击“显示更多评论”按钮来显示所有评论?

我做了什么:

  1. 为了抓取所有评论,我决定继续点击直到该按钮消失。
  2. 但有些新评论点击8次后没有出现(使用下面的while)。
  3. 我已经检查了按钮的 xpath 但它没有改变。
  4. 如果我在没有驱动程序的情况下手动单击按钮,我可以看到新的评论。

此评论的堆栈(点击按钮 8 次后即可看到)

import time, random
from selenium import webdriver
from latest_user_agents import get_random_user_agent

### ser user_agent
user_agent = get_random_user_agent()
options = webdriver.ChromeOptions()
options.add_argument("--user-agent=" + user_agent)
driverpath = "C:/Users/~~/chromedriver.exe"
driver = webdriver.Chrome(chrome_options=options,executable_path=driverpath)

### target url
url = "https://www.sony.co.uk/electronics/truly-wireless/wf-l900/reviews-ratings"

### open URL
driver.get(url)
time.sleep(5)

# accept cookies
driver.find_element_by_xpath('//*[@id="onetrust-accept-btn-handler"]').click()

# show all reviews
try:
    while True:
        driver.execute_script('window.scroll(0,1000000);') 
        driver.find_element_by_xpath('//*[@id="reviews_listing_278405943492055451029662"]/div[3]/div[4]/button').click()
        time.sleep(random.randrange(2, 5, 1))
except:
    print("---------------- finish showing all reviews ----------------")

Question:

How can I show all reviews by clicking "Show more reviews" button?

What did I do:

  1. To scrape all reviews, I decided to Keep clicking until that button disappears.
  2. But some new reviews didn't appear after clicking for 8 times (using while below).
  3. I already checked xpath of the button but it didn't changed.
  4. If I click the button manually without the driver, I can see new reviews.

stack from this review (can see after clicking the button for 8 times)

import time, random
from selenium import webdriver
from latest_user_agents import get_random_user_agent

### ser user_agent
user_agent = get_random_user_agent()
options = webdriver.ChromeOptions()
options.add_argument("--user-agent=" + user_agent)
driverpath = "C:/Users/~~/chromedriver.exe"
driver = webdriver.Chrome(chrome_options=options,executable_path=driverpath)

### target url
url = "https://www.sony.co.uk/electronics/truly-wireless/wf-l900/reviews-ratings"

### open URL
driver.get(url)
time.sleep(5)

# accept cookies
driver.find_element_by_xpath('//*[@id="onetrust-accept-btn-handler"]').click()

# show all reviews
try:
    while True:
        driver.execute_script('window.scroll(0,1000000);') 
        driver.find_element_by_xpath('//*[@id="reviews_listing_278405943492055451029662"]/div[3]/div[4]/button').click()
        time.sleep(random.randrange(2, 5, 1))
except:
    print("---------------- finish showing all reviews ----------------")

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花想c2025-01-27 08:58:46

尝试转到网页末尾,然后执行此操作,因为按钮可能未完全加载。

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

您也可以尝试仅在该按钮可用时才按下该按钮。

element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "myElement")))

Try going to the end of the webpage and then do it as the button might not been loaded fully.

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

You might also try to press the button only when it is available.

element = WebDriverWait(driver, 20).until(
EC.presence_of_element_located((By.ID, "myElement")))
野鹿林2025-01-27 08:58:46

您可以尝试 javascript click:

element = driver.find_element_by_xpath('//*[@id="reviews_listing_278405943492055451029662"]/div[3]/div[4]/button')
driver.execute_script("arguments[0].click();", element)

在此方法中,不需要滚动。 此处查看有关该主题的更多信息。

You can try javascript click:

element = driver.find_element_by_xpath('//*[@id="reviews_listing_278405943492055451029662"]/div[3]/div[4]/button')
driver.execute_script("arguments[0].click();", element)

In this method, the scrolling shouldn't be necessary. See more info about the subject here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文