Prendre une capture d'écran de la page entière avec le Sélénium Python avec chromedriver

Après avoir essayé différentes approches... je suis tombé sur cette page pour prendre fullpage capture d'écran avec chromedriver, le sélénium et le python.

code original ici: http://seleniumpythonqa.blogspot.com/2015/08/generate-full-page-screenshot-in-chrome.html (et je copie le code dans cette annonce ci-dessous)

Il utilise PIL et il fonctionne très bien!!!!! Cependant il y a une question... qui est elle capte fixe les en-têtes et les répétitions pour l'ensemble de la page et aussi manque certaines parties de la page lors de changement de page. exemple d'url pour prendre une capture d'écran:

http://www.w3schools.com/js/default.asp

Comment éviter la répétition des en-têtes avec ce code... Ou est-il la meilleure option qui utilise python... ( je ne sais pas java et ne souhaitez pas utiliser java).

Veuillez voir la capture d'écran du résultat courant et de l'exemple de code ci-dessous.

Prendre une capture d'écran de la page entière avec le Sélénium Python avec chromedriver

test.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]])

util.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
Je vais prendre une capture d'écran d'une page qui nécessite plusieurs manuscrits et de couture. Malheureusement, ce n'est pas une URL publique (vous ne pouvez voir la page si vous êtes connecté). Savez-vous pourquoi il garde ajout de l'en-tête ainsi? res.cloudinary.com/mpyr-com/image/upload/v1551372542/...

OriginalL'auteur ihightower | 2017-01-18