【发布时间】: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