【发布时间】:2018-06-23 18:45:45
【问题描述】:
关于这个主题有很多东西可以找到,但无法弄清楚。我需要滚动到(不是很长)无限滚动页面的末尾。我有 2 个选项可以与 chrome non-headless 一起使用,但似乎无法使用 headless。
我最喜欢的第一个,做工漂亮,在 SA 上找到:
driver = webdriver.Chrome('c:/cd.exe', chrome_options=chrome_options)
driver.get('http://www.website.com')
while True:
count = len(driver.find_elements_by_xpath('//div[@itemprop="itemListElement"]'))
print(count)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
try:
WebDriverWait(driver, 50).until(EC.visibility_of_element_located((By.XPATH,
"//div[@itemprop='itemListElement'][%s]" % str(count + 1))))
except TimeoutException:
break
在意识到我无法在无头模式下摆脱上述情况后进行第二次黑客工作:
driver = webdriver.Chrome('c:/cd.exe', chrome_options=chrome_options)
driver.get('https://www.website.com')
while True:
count = len(driver.find_elements_by_xpath('//div[@itemprop="itemListElement"]'))
actions = ActionChains(driver)
actions.send_keys(Keys.PAGE_DOWN)
actions.perform()
actions.send_keys(Keys.PAGE_DOWN)
actions.perform()
# focus_element_scroll = driver.find_elements_by_xpath('//section[@class="occasion-content"]')
# driver.find_elements_by_xpath('//div[@itemprop="itemListElement"]')[-1].send_keys(Keys.PAGE_DOWN)
# driver.find_elements_by_xpath('//div[@itemprop="itemListElement"]')[-1].send_keys(Keys.PAGE_DOWN)
# self.driver.find_element_by_css_selector("ul.list-with-results").send_keys(Keys.ARROW_DOWN)
print(count)
# driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
try:
WebDriverWait(driver, 50).until(EC.visibility_of_element_located((By.XPATH,
"//div[@itemprop='itemListElement'][%s]" % str(count + 1))))
except TimeoutException:
break
所以两者都在 chrome 中工作,但不在无头模式下,我需要将它们推送到需要无头的 ubuntu vps,我知道 xvfb 选项,但我很高兴我可以删除它并使用原生 chrome因为水滴没有太多的记忆。
编辑:刚刚尝试了这种方法,专注于页脚中的一个元素,也适用于非无头但不适用于无头:
ActionChains(driver).move_to_element(focus[0]).perform()
有人有不同的方法吗?
编辑只是想知道是否可以在无头模式下使用chrome滚动!
【问题讨论】:
标签: python google-chrome selenium headless