title: Take screenshot of full page with Selenium Python with chromedriver
body: <p>After trying out various approaches... I have stumbled upon this page to take full-page screenshot with chromedriver, selenium and python.</p>
  
  <p>The original code is <a href="http://seleniumpythonqa.blogspot.com/2015/08/generate-full-page-screenshot-in-chrome.html" rel="noreferrer">here</a>. (and I copy the code in this posting below)</p>
  
  <p>It uses PIL and it works great! However, there is one issue... which is it captures fixed headers and repeats for the whole page and also misses some parts of the page during page change. sample url to take a screenshot: </p>
  
  <p><a href="http://www.w3schools.com/js/default.asp" rel="noreferrer">http://www.w3schools.com/js/default.asp</a></p>
  
  <p><strong>How to avoid the repeated headers with this code... Or is there any better option which uses python only...</strong> <strong>( i don't know java and do not want to use java).</strong></p>
  
  <p>Please see the screenshot of the current result and sample code below.</p>
  
  <p><a href="https://i.stack.imgur.com/PSutD.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/PSutD.jpg" alt="full page screenshot with repeated headers"></a></p>
  
  <p><strong>test.py</strong></p>
  
  <pre><code>"""
  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]])
  </code></pre>
  
  <p><strong>util.py</strong></p>
  
  <pre><code>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 &lt; total_height:
              ii = 0
              top_height = i + viewport_height
  
              if top_height &gt; total_height:
                  top_height = total_height
  
              while ii &lt; total_width:
                  top_width = ii + viewport_width
  
                  if top_width &gt; 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 &gt; 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
  </code></pre>