试图使用python上的硒滚动弹出弹出式弹出弹出时,对象无法相互作用

发布于 2025-01-31 04:23:02 字数 1953 浏览 1 评论 0原文

我需要向下滚动以下链接中出现的弹出窗口 https://www.vivino.com/it/en/en/rronchi-di-cialla-picolit-di-cialla/w/2015576?year=2011&pprice_id = 23500586 (或Vivino上的任何其他葡萄酒链接)单击“显示更多评论”后,但是当我尝试滚动时,我会发现元素无法相互作用的错误。 弹出评论的代码是以下

from urllib.request import urlretrieve
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import pandas as pd
import csv
import re
import time
import random



target_url = 'https://www.vivino.com/IT/it/ronchi-di-cialla-picolit-di-cialla/w/2015576?year=2011&price_id=23500586'
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)




driver = webdriver.Chrome(options=options)
driver.get(target_url) #apre la pagina

last_height = driver.execute_script("return document.body.scrollHeight") 

while True:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight/3);")
    time.sleep(1)
    driver.execute_script("window.scrollTo(document.body.scrollHeight/3, document.body.scrollHeight*2/3);")
    time.sleep(1)
    driver.execute_script("window.scrollTo(document.body.scrollHeight*2/3, document.body.scrollHeight);")
    
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
    
driver.find_element(By.CSS_SELECTOR, "button[class='MuiButtonBase-root MuiButton-root jss1 MuiButton-outlined jss3 MuiButton-disableElevation']").click()

I need to scroll down a pop up that appears in the following link https://www.vivino.com/IT/en/ronchi-di-cialla-picolit-di-cialla/w/2015576?year=2011&price_id=23500586 (or any other wine link on vivino) after clicking on 'show more reviews', but when i try to scroll i get the Element not Interactable error.
The code to open the reviews pop up is the following

from urllib.request import urlretrieve
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
import pandas as pd
import csv
import re
import time
import random



target_url = 'https://www.vivino.com/IT/it/ronchi-di-cialla-picolit-di-cialla/w/2015576?year=2011&price_id=23500586'
options = webdriver.ChromeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)




driver = webdriver.Chrome(options=options)
driver.get(target_url) #apre la pagina

last_height = driver.execute_script("return document.body.scrollHeight") 

while True:
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight/3);")
    time.sleep(1)
    driver.execute_script("window.scrollTo(document.body.scrollHeight/3, document.body.scrollHeight*2/3);")
    time.sleep(1)
    driver.execute_script("window.scrollTo(document.body.scrollHeight*2/3, document.body.scrollHeight);")
    
    new_height = driver.execute_script("return document.body.scrollHeight")
    if new_height == last_height:
        break
    last_height = new_height
    
driver.find_element(By.CSS_SELECTOR, "button[class='MuiButtonBase-root MuiButton-root jss1 MuiButton-outlined jss3 MuiButton-disableElevation']").click()

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

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

发布评论

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

评论(1

梦罢 2025-02-07 04:23:02

这是每1秒钟向下滚动到每个评论的代码。我建议您不要直接在评论上循环,即不要做类似的事情

reviews = driver.find_elements(By.XPATH, "//div[contains(@class, 'allReviews__reviews')]/child::div")
for review in reviews:
    ...

,因为最初只加载了很少的评论,然后随着您向下滚动时,会加载更多的评论。因此,如果您使用上述代码评论将仅包含第一个加载的评论。下面的代码解决了此问题,但是如果总评论的数量小于limit,则将失败。因此,您应该改进代码以考虑到这一事实

target_url = 'https://www.vivino.com/IT/it/ronchi-di-cialla-picolit-di-cialla/w/2015576?year=2011&price_id=23500586'
driver.get(target_url)
# go to reviews section
driver.get(target_url + '#all_reviews')
# click on show more reviews
driver.find_element(By.XPATH, "//span[contains(text(), 'Mostra altre recensioni')]/../..").click()
# scroll down to each review, one by one in a loop
limit = 20
for idx in range(limit):
    reviews = driver.find_elements(By.XPATH, "//div[contains(@class, 'allReviews__reviews')]/child::div")
    driver.execute_script('arguments[0].scrollIntoView({block: "center", behavior: "smooth"});', reviews[idx])
    time.sleep(1)

Here is the code scrolling down to each review every 1 second. I suggest you to not loop directly on reviews, i.e. don't do something like

reviews = driver.find_elements(By.XPATH, "//div[contains(@class, 'allReviews__reviews')]/child::div")
for review in reviews:
    ...

because initially only few reviews are loaded, and then as you scroll down more are loaded. So if you use the code above reviews will only contain the first loaded reviews. The code below solves this problem, but it will fail if the number of total reviews is smaller than limit. So you should improve the code to take into account also this fact

target_url = 'https://www.vivino.com/IT/it/ronchi-di-cialla-picolit-di-cialla/w/2015576?year=2011&price_id=23500586'
driver.get(target_url)
# go to reviews section
driver.get(target_url + '#all_reviews')
# click on show more reviews
driver.find_element(By.XPATH, "//span[contains(text(), 'Mostra altre recensioni')]/../..").click()
# scroll down to each review, one by one in a loop
limit = 20
for idx in range(limit):
    reviews = driver.find_elements(By.XPATH, "//div[contains(@class, 'allReviews__reviews')]/child::div")
    driver.execute_script('arguments[0].scrollIntoView({block: "center", behavior: "smooth"});', reviews[idx])
    time.sleep(1)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文