【问题标题】:Selenium - Scroll for screenshotsSelenium - 滚动屏幕截图
【发布时间】:2020-12-18 17:12:43
【问题描述】:

我有一个通过 UI 并填写很多页面的 python 脚本。我截图了,但是如果页面比屏幕长,那么我错过了一半的页面。我尝试了更长的长度,但是对于普通页面,它很荒谬且无法使用。我尝试使用 ashot,但不知道如何将其添加到我在 pycharm 中的项目中。

有什么智慧或其他想法吗?谢谢。

更新尝试但不起作用的事情(页面上没有发生任何事情:

actions = ActionChains(driver)
actions.move_to_element(continuebtn)

还有:

chrome_options.add_argument('--enable-javascript')
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
driver.execute_script("window.scrollTo(0, 1080);")

【问题讨论】:

  • 好吧,这很糟糕,@DebanjanB - 你关闭了我的问题并为 JAVA 链接了两个“解决方案”。我正在使用 Python
  • 重新打开问题。

标签: python selenium screenshot


【解决方案1】:

您可以截取多个屏幕截图并滚动浏览整个页面。

一个简单的例子是:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.firefox.options import Options
import time

d = webdriver.Firefox(executable_path="PATH TO YOUR DRIVER")
d.get("https://en.wikipedia.org/wiki/HTTP_cookie")
time.sleep(1)
scrollHeight = d.execute_script("return window.scrollMaxY")
index = 0
while d.execute_script("return window.pageYOffset") < scrollHeight:
    d.save_screenshot(PICTURE_PATH+"/"+ FILENAME + str(index).zfill(2) + ".png")
    d.execute_script("window.scrollByPages(1)")
    # maybe call time.sleep in here somewhere to load lazyloaded images
    time.sleep(0.2)
    index += 1
d.quit()

【讨论】:

  • 我尝试了以下方法(其中驱动程序是我的 chrome 浏览器实例)只是为了查看是否可以让它滚动页面并且页面上没有任何反应: driver.execute_script("window.scrollTo(0 , document.body.scrollHeight);")
  • 你用firefox试过这个方法吗?或者,您是否尝试过在选项中手动添加 javascript(请参阅https://stackoverflow.com/questions/55480924/how-to-enable-javascript-in-selenium-webdriver-chrome-using-python
  • 我们不使用火狐。仅限 Chrome 和 IE。我会查看链接 - 谢谢!
  • 该链接上的两个项目不起作用。一个没有改变,一个导致错误。不过谢谢:/
  • 我想我成功了!我必须点击页面上的其他地方才能真正起作用。
猜你喜欢
  • 2020-05-24
  • 1970-01-01
  • 2011-10-13
  • 2016-02-12
  • 1970-01-01
  • 1970-01-01
  • 2019-02-08
  • 2014-08-25
  • 1970-01-01
相关资源
最近更新 更多