在尝试了各种方法之后。。。我无意中发现了这个页面,用chromedriver、selenium和python截图。
原始代码是here。(我复制了下面这篇文章中的代码)
太好了!然而,有一个问题。。。也就是说,它捕获了整个页面的固定标题和重复,并且在页面更改期间丢失了页面的某些部分。截图的示例url:http://www.w3schools.com/js/default.asp
如何使用此代码避免重复的标题。。。或者有没有更好的选项只使用python…(我不懂java,不想使用java)。
请参阅下面的当前结果和示例代码的屏幕截图。
测试.py
"""
This script uses a simplified version of the one here:
https://snipt.net/restrada/python-selenium-workaround-for-full-page-screenshot-using-chromedriver-2x/
It contains the *crucial* correction added in the comments by Jason Coutu.
"""
import sys
from selenium import webdriver
import unittest
import util
class Test(unittest.TestCase):
""" Demonstration: Get Chrome to generate fullscreen screenshot """
def setUp(self):
self.driver = webdriver.Chrome()
def tearDown(self):
self.driver.quit()
def test_fullpage_screenshot(self):
''' Generate document-height screenshot '''
#url = "http://effbot.org/imagingbook/introduction.htm"
url = "http://www.w3schools.com/js/default.asp"
self.driver.get(url)
util.fullpage_screenshot(self.driver, "test.png")
if __name__ == "__main__":
unittest.main(argv=[sys.argv[0]])
实用py
import os
import time
from PIL import Image
def fullpage_screenshot(driver, file):
print("Starting chrome full page screenshot workaround ...")
total_width = driver.execute_script("return document.body.offsetWidth")
total_height = driver.execute_script("return document.body.parentNode.scrollHeight")
viewport_width = driver.execute_script("return document.body.clientWidth")
viewport_height = driver.execute_script("return window.innerHeight")
print("Total: ({0}, {1}), Viewport: ({2},{3})".format(total_width, total_height,viewport_width,viewport_height))
rectangles = []
i = 0
while i < total_height:
ii = 0
top_height = i + viewport_height
if top_height > total_height:
top_height = total_height
while ii < total_width:
top_width = ii + viewport_width
if top_width > total_width:
top_width = total_width
print("appending rectangle ({0},{1},{2},{3})".format(ii, i, top_width, top_height))
rectangles.append((ii, i, top_width,top_height))
ii = ii + viewport_width
i = i + viewport_height
stitched_image = Image.new('RGB', (total_width, total_height))
previous = None
part = 0
for rectangle in rectangles:
if not previous is None:
driver.execute_script("window.scrollTo({0}, {1})".format(rectangle[0], rectangle[1]))
print("Scrolled To ({0},{1})".format(rectangle[0], rectangle[1]))
time.sleep(0.2)
file_name = "part_{0}.png".format(part)
print("Capturing {0} ...".format(file_name))
driver.get_screenshot_as_file(file_name)
screenshot = Image.open(file_name)
if rectangle[1] + viewport_height > total_height:
offset = (rectangle[0], total_height - viewport_height)
else:
offset = (rectangle[0], rectangle[1])
print("Adding to stitched image with offset ({0}, {1})".format(offset[0],offset[1]))
stitched_image.paste(screenshot, offset)
del screenshot
os.remove(file_name)
part = part + 1
previous = rectangle
stitched_image.save(file)
print("Finishing chrome full page screenshot workaround...")
return True
工作原理:尽可能长地设置浏览器高度。。。
这个答案通过am05mhz和Javed Karim改进了先前的答案。
它采用无头模式,并且最初没有设置窗口大小选项。在调用此函数之前,请确保页已完全或充分加载。
它试图将宽度和高度都设置为所需的值。整个页面的屏幕截图有时可能包含不必要的垂直滚动条。通常避免滚动条的一种方法是截取body元素的屏幕快照。保存屏幕截图后,它会将大小还原为原来的大小,否则下一个屏幕截图的大小可能设置不正确。
最后,对于一些例子来说,这种技术可能仍然不能很好地工作。
如果使用早于3.6的Python,请从函数定义中删除类型注释。
这对我有用。它将整个页面保存为屏幕截图。 有关更多信息,请阅读api文档: http://selenium-python.readthedocs.io/api.html
相关问题 更多 >
编程相关推荐