【问题标题】:How to scroll down google maps using selenium python如何使用 selenium python 向下滚动谷歌地图
【发布时间】:2022-06-22 16:47:30
【问题描述】:

我正在尝试使用 selenium 向下滚动谷歌地图页面,但无法这样做。我已经尝试了这里写的所有东西: Scroll down google maps webpage using selenium python

这是我的代码:

def scroll(self):        
    SCROLL_PAUSE_TIME = 5

    # Get scroll height
    last_height = self.execute_script("return document.body.scrollHeight") 

    number = 0

    while True:
        number = number+1

        # Scroll down to bottom
        
        ele = self.find_element_by_css_selector('div[jstcache="211"]')
        print(ele)
        self.execute_script('arguments[0].scrollBy(0, 500)', ele)

        # Wait to load page

        time.sleep(SCROLL_PAUSE_TIME)

        # Calculate new scroll height and compare with last scroll height
        print(f'last height: {last_height}')

        ele = self.find_element_by_css_selector('div[jstcache="211"]')
        
        new_height = self.execute_script("return arguments[0].scrollHeight", ele) 

        print(f'new height: {new_height}')

        if number == 5:
            break

        if new_height == last_height:
            break

        print('cont')
        last_height = new_height

但不知道如何向下滚动。

请帮忙!!

【问题讨论】:

  • 你自己试过了吗?你有什么错误吗?我们可以看看你的代码吗?
  • 请提供足够的代码,以便其他人更好地理解或重现问题。
  • @Akzy 我已经添加了代码,请看看有什么问题

标签: python selenium


【解决方案1】:

进入网站后,您很可能会打开侧面板。通过 selenium 一次单击屏幕中心来关闭它。然后将箭头键发送到地图元素。

from selenium.webdriver.common.keys import Keys

driver.find_element(BY.CLASS, "dmCR2e widget-scene").click()
driver.find_element(BY.CLASS, "dmCR2e widget-scene").send_keys(Keys.ARROW_DOWN) 

#ARROW_UP ARROW_RIGHT ARROW_LEFT

【讨论】:

  • 显然“dmCR2e widget-scene”是地图的类别,它可能取决于我的位置,所以在继续之前请确保。
  • 哦,是的,另外,如果您想连续滚动,请不要忘记 for/while 循环。
  • 我想滚动那个侧面板
  • 但是没有什么可以滚动的
  • 如果你想从侧面板获取一些东西,你不必滚动,因为所有的 web 元素都已经在 HTML 源中,除非你想看到它。
【解决方案2】:

我使用了相同的代码,您可以通过删除以下行来修复它:)

    if number == 5:
    break

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 2022-10-05
    • 1970-01-01
    • 1970-01-01
    • 2019-09-26
    • 2019-03-29
    • 1970-01-01
    相关资源
    最近更新 更多