【问题标题】:scrolling through an element in python3+selenium with chrome webdriver on google maps使用谷歌地图上的chrome webdriver滚动python3 + selenium中的元素
【发布时间】:2019-06-20 03:30:02
【问题描述】:

我想使用 selenium chrome webdriver 在谷歌地图中滚动浏览一些关于餐厅的评论,在这里说 Nobu Palo Alto:
https://www.google.com/maps/place/Nobu+Palo+Alto/@37.4437223,-122.1637038,17z/data=!3m1!4b1!4m11!1m3!2m2!1srestaurants!6e5!3m6!1s0x0:0x5bb11772add3928!8m2!3d37.4437179!4d-122.1615154!9m1!1b1

我使用了这个似乎获取(并打印)javascript高度的函数,但它不是无限滚动,而是在打印最后一个高度==新高度后中断,但我知道它没有加载更多评论:

def __init__(self, site):
    self.site=site
    self.option = webdriver.ChromeOptions()
    self.option.add_argument("-incognito")
    self.browser = webdriver.Chrome(executable_path="C:/Users/me/Documents/project/chromedriver.exe",chrome_options=self.option)

def scroll(self):
    self.browser.get(self.site)
    SCROLL_PAUSE_TIME = 4
    sleep(SCROLL_PAUSE_TIME)
    # Get scroll height
    last_height = self.browser.execute_script("return document.querySelector('#pane > div > div.widget-pane-content.scrollable-y > div > div > div.section-listbox.section-scrollbox.scrollable-y.scrollable-show').scrollHeight")
    print("last height = " + str(last_height))

    while True:
        # Scroll down to bottom
        self.browser.execute_script("window.scrollTo(0, document.querySelector('#pane > div > div.widget-pane-content.scrollable-y > div > div > div.section-listbox.section-scrollbox.scrollable-y.scrollable-show').scrollHeight);")

        # Wait to load page
        sleep(SCROLL_PAUSE_TIME)

        # Calculate new scroll height and compare with last scroll height
        new_height = self.browser.execute_script("return document.querySelector('#pane > div > div.widget-pane-content.scrollable-y > div > div > div.section-listbox.section-scrollbox.scrollable-y.scrollable-show').scrollHeight")
        print("new height = " + str(new_height))
        if new_height == last_height:
            break
        last_height = new_height

【问题讨论】:

    标签: python python-3.x selenium selenium-webdriver selenium-chromedriver


    【解决方案1】:

    您不必使用 javascript 滚动浏览来加载评论。

    这是加载所需评论数量的简单脚本。

    reviewCount = len(driver.find_elements_by_xpath("//div[@class='section-review ripple-container']"))
    # loading a minimum of 50 reviews
    while reviewCount <50: #<=== change this number based on your requirement
        # load the reviews
        driver.find_element_by_xpath("//div[contains(@class,'section-loading-spinner')]").location_once_scrolled_into_view 
        # wait for loading the reviews
        WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//div[@class='section-loading-overlay-spinner'][@style='display:none']")))
        # get the reviewsCount
        reviewCount = len(driver.find_elements_by_xpath("//div[@class='section-review ripple-container']"))
    

    【讨论】:

    • 我认为这种方法比我想的要好,以后会使用它。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    • 2015-04-12
    • 2022-06-22
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 2017-01-03
    相关资源
    最近更新 更多