python - Selenium - 滚动屏幕截图
问题描述
我有一个通过 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);")
解决方案
您可以截取多个屏幕截图并滚动浏览整个页面。
一个简单的例子是:
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()
推荐阅读
- database-migration - 在 Liquibase 中,迁移/变更集数量的合理限制是什么
- angular - 为什么 Async Await 不能与 Angular @Action 一起使用
- javascript - 无法读取 vue-jquery 中未定义的属性“createDocumentFragment”
- google-cloud-platform - 如何通过 Terraform 管理 BigQuery 表的架构更改
- styled-components - 我想使用从样式组件中获得的 url 作为道具
- windows - “npm 通知创建了一个锁文件作为 package-lock.json。你应该提交这个文件。”
- github - GitHub 我无法向拉取请求添加审阅者
- vue.js - 如何隐藏或删除 bootstrap-vue b-table 中的标头字段?
- android - 在 Firebase 端而不是在 Android 代码中加密数据
- c++ - C++ 反向“for”循环