{"id":336,"date":"2017-12-18T15:43:20","date_gmt":"2017-12-18T20:43:20","guid":{"rendered":"https:\/\/arthurpemberton.com\/?p=336"},"modified":"2018-12-26T22:38:51","modified_gmt":"2018-12-27T03:38:51","slug":"full-page-screenshots-with-python-and-selenium","status":"publish","type":"post","link":"https:\/\/arthurpemberton.com\/2017\/12\/full-page-screenshots-with-python-and-selenium","title":{"rendered":"Full page screenshots with Python and Selenium"},"content":{"rendered":"<p>Currently, none of the major Selenium drivers (browsers) support the ability to easily take a screenshot of an entire web page. The following function takes multiple screenshots through the viewport and scrolls between screenshots, then stitches the resulting images into a single PNG.<\/p>\n<p><!--more--><\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef save_fullpage_screenshot(driver, url, output_path, tmp_prefix='selenium_screenshot', tmp_suffix='.png'):\r\n\t&quot;&quot;&quot;\r\n\tCreates a full page screenshot using a selenium driver by scrolling and taking multiple screenshots,\r\n\tand stitching them into a single image.\r\n\t&quot;&quot;&quot;\r\n\r\n\t# get the page\r\n\tdriver.get(url)\r\n\r\n\t# get dimensions\r\n\twindow_height = driver.execute_script('return window.innerHeight')\r\n\tscroll_height = driver.execute_script('return document.body.parentNode.scrollHeight')\r\n\tnum = int( math.ceil( float(scroll_height) \/ float(window_height) ) )\r\n\r\n\t# get temp files\r\n\ttempfiles = &#x5B;]\r\n\tfor i in xrange( num ):\r\n\t\tfd,path = tempfile.mkstemp(prefix='{0}-{1:02}-'.format(tmp_prefix, i+1), suffix=tmp_suffix)\r\n\t\tos.close(fd)\r\n\t\ttempfiles.append(path)\r\n\t\tpass\r\n\r\n\ttry:\r\n\t\t# take screenshots\r\n\t\tfor i,path in enumerate(tempfiles):\r\n\t\t\tif i &gt; 0:\r\n\t\t\t\tdriver.execute_script( 'window.scrollBy(%d,%d)' % (0, window_height) )\r\n\t\t\t\r\n\t\t\tdriver.save_screenshot(path)\r\n\t\t\tpass\r\n\t\t\r\n\t\t# stitch images together\r\n\t\tstiched = None\r\n\t\tfor i,path in enumerate(tempfiles):\r\n\t\t\timg = Image.open(path)\r\n\t\t\t\r\n\t\t\tw, h = img.size\r\n\t\t\ty = i * window_height\r\n\t\t\t\r\n\t\t\tif i == ( len(tempfiles) - 1 ):\r\n\t\t\t\timg = img.crop((0, h-(scroll_height % h), w, h))\r\n\t\t\t\tw, h = img.size\r\n\t\t\t\tpass\r\n\t\t\t\r\n\t\t\tif stiched is None:\r\n\t\t\t\tstiched = Image.new('RGB', (w, scroll_height))\r\n\t\t\t\r\n\t\t\tstiched.paste(img, (\r\n\t\t\t\t0, # x0\r\n\t\t\t\ty, # y0\r\n\t\t\t\tw, # x1\r\n\t\t\t\ty + h # y1\r\n\t\t\t))\r\n\t\t\tpass\r\n\t\tstiched.save(output_path)\r\n\tfinally:\r\n\t\t# cleanup\r\n\t\tfor path in tempfiles:\r\n\t\t\tif os.path.isfile(path):\r\n\t\t\t\tos.remove(path)\r\n\t\tpass\r\n\r\n\treturn output_path\r\n<\/pre>\n<p>The following libraries are required:<\/p>\n<ul>\n<li>selenium<\/li>\n<li>Pillow<\/li>\n<\/ul>\n<hr \/>\n<p>Slightly updated version <a href=\"https:\/\/arthurpemberton.com\/2018\/12\/full-page-screenshots-with-python-3-and-selenium\">here<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Currently, none of the major Selenium drivers (browsers) support the ability to easily take a screenshot of an entire web page. Here&#8217;s a function to overcome that limitation.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42],"tags":[47],"class_list":["post-336","post","type-post","status-publish","format-standard","hentry","category-python","tag-automation"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Full page screenshots with Python and Selenium - Arthur Pemberton<\/title>\n<meta name=\"description\" content=\"Selenium drivers do not support the ability to take a screenshot of an entire web page. Here&#039;s a function to overcome that limitation.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/arthurpemberton.com\/2017\/12\/full-page-screenshots-with-python-and-selenium\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Full page screenshots with Python and Selenium - Arthur Pemberton\" \/>\n<meta property=\"og:description\" content=\"Selenium drivers do not support the ability to take a screenshot of an entire web page. Here&#039;s a function to overcome that limitation.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/arthurpemberton.com\/2017\/12\/full-page-screenshots-with-python-and-selenium\" \/>\n<meta property=\"og:site_name\" content=\"Arthur Pemberton\" \/>\n<meta property=\"article:published_time\" content=\"2017-12-18T20:43:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-12-27T03:38:51+00:00\" \/>\n<meta name=\"author\" content=\"Arthur Pemberton\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Arthur Pemberton\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/arthurpemberton.com\\\/2017\\\/12\\\/full-page-screenshots-with-python-and-selenium#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/arthurpemberton.com\\\/2017\\\/12\\\/full-page-screenshots-with-python-and-selenium\"},\"author\":{\"name\":\"Arthur Pemberton\",\"@id\":\"https:\\\/\\\/arthurpemberton.com\\\/#\\\/schema\\\/person\\\/e4504c0e198fc8188db87092450f6367\"},\"headline\":\"Full page screenshots with Python and Selenium\",\"datePublished\":\"2017-12-18T20:43:20+00:00\",\"dateModified\":\"2018-12-27T03:38:51+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/arthurpemberton.com\\\/2017\\\/12\\\/full-page-screenshots-with-python-and-selenium\"},\"wordCount\":287,\"commentCount\":9,\"publisher\":{\"@id\":\"https:\\\/\\\/arthurpemberton.com\\\/#\\\/schema\\\/person\\\/e4504c0e198fc8188db87092450f6367\"},\"keywords\":[\"automation\"],\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/arthurpemberton.com\\\/2017\\\/12\\\/full-page-screenshots-with-python-and-selenium#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/arthurpemberton.com\\\/2017\\\/12\\\/full-page-screenshots-with-python-and-selenium\",\"url\":\"https:\\\/\\\/arthurpemberton.com\\\/2017\\\/12\\\/full-page-screenshots-with-python-and-selenium\",\"name\":\"Full page screenshots with Python and Selenium - Arthur Pemberton\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/arthurpemberton.com\\\/#website\"},\"datePublished\":\"2017-12-18T20:43:20+00:00\",\"dateModified\":\"2018-12-27T03:38:51+00:00\",\"description\":\"Selenium drivers do not support the ability to take a screenshot of an entire web page. Here's a function to overcome that limitation.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/arthurpemberton.com\\\/2017\\\/12\\\/full-page-screenshots-with-python-and-selenium\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/arthurpemberton.com\\\/#website\",\"url\":\"https:\\\/\\\/arthurpemberton.com\\\/\",\"name\":\"Arthur Pemberton\",\"description\":\"The professional website of Arthur Pemberton\",\"publisher\":{\"@id\":\"https:\\\/\\\/arthurpemberton.com\\\/#\\\/schema\\\/person\\\/e4504c0e198fc8188db87092450f6367\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/arthurpemberton.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/arthurpemberton.com\\\/#\\\/schema\\\/person\\\/e4504c0e198fc8188db87092450f6367\",\"name\":\"Arthur Pemberton\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23f8dddfae2b25e80c1bf8fd2c90661b8d2079f05446bcb0d0973a5a92570536?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23f8dddfae2b25e80c1bf8fd2c90661b8d2079f05446bcb0d0973a5a92570536?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23f8dddfae2b25e80c1bf8fd2c90661b8d2079f05446bcb0d0973a5a92570536?s=96&d=mm&r=g\",\"caption\":\"Arthur Pemberton\"},\"logo\":{\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/23f8dddfae2b25e80c1bf8fd2c90661b8d2079f05446bcb0d0973a5a92570536?s=96&d=mm&r=g\"},\"description\":\"A seasoned web applications developer with over seven years of professional experience designing, developing, and deploying end-to-end solutions; and over ten years administering websites.\",\"sameAs\":[\"http:\\\/\\\/arthurpemberton.com\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/arthurpemberton\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Full page screenshots with Python and Selenium - Arthur Pemberton","description":"Selenium drivers do not support the ability to take a screenshot of an entire web page. Here's a function to overcome that limitation.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/arthurpemberton.com\/2017\/12\/full-page-screenshots-with-python-and-selenium","og_locale":"en_US","og_type":"article","og_title":"Full page screenshots with Python and Selenium - Arthur Pemberton","og_description":"Selenium drivers do not support the ability to take a screenshot of an entire web page. Here's a function to overcome that limitation.","og_url":"https:\/\/arthurpemberton.com\/2017\/12\/full-page-screenshots-with-python-and-selenium","og_site_name":"Arthur Pemberton","article_published_time":"2017-12-18T20:43:20+00:00","article_modified_time":"2018-12-27T03:38:51+00:00","author":"Arthur Pemberton","twitter_misc":{"Written by":"Arthur Pemberton","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/arthurpemberton.com\/2017\/12\/full-page-screenshots-with-python-and-selenium#article","isPartOf":{"@id":"https:\/\/arthurpemberton.com\/2017\/12\/full-page-screenshots-with-python-and-selenium"},"author":{"name":"Arthur Pemberton","@id":"https:\/\/arthurpemberton.com\/#\/schema\/person\/e4504c0e198fc8188db87092450f6367"},"headline":"Full page screenshots with Python and Selenium","datePublished":"2017-12-18T20:43:20+00:00","dateModified":"2018-12-27T03:38:51+00:00","mainEntityOfPage":{"@id":"https:\/\/arthurpemberton.com\/2017\/12\/full-page-screenshots-with-python-and-selenium"},"wordCount":287,"commentCount":9,"publisher":{"@id":"https:\/\/arthurpemberton.com\/#\/schema\/person\/e4504c0e198fc8188db87092450f6367"},"keywords":["automation"],"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/arthurpemberton.com\/2017\/12\/full-page-screenshots-with-python-and-selenium#respond"]}]},{"@type":"WebPage","@id":"https:\/\/arthurpemberton.com\/2017\/12\/full-page-screenshots-with-python-and-selenium","url":"https:\/\/arthurpemberton.com\/2017\/12\/full-page-screenshots-with-python-and-selenium","name":"Full page screenshots with Python and Selenium - Arthur Pemberton","isPartOf":{"@id":"https:\/\/arthurpemberton.com\/#website"},"datePublished":"2017-12-18T20:43:20+00:00","dateModified":"2018-12-27T03:38:51+00:00","description":"Selenium drivers do not support the ability to take a screenshot of an entire web page. Here's a function to overcome that limitation.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/arthurpemberton.com\/2017\/12\/full-page-screenshots-with-python-and-selenium"]}]},{"@type":"WebSite","@id":"https:\/\/arthurpemberton.com\/#website","url":"https:\/\/arthurpemberton.com\/","name":"Arthur Pemberton","description":"The professional website of Arthur Pemberton","publisher":{"@id":"https:\/\/arthurpemberton.com\/#\/schema\/person\/e4504c0e198fc8188db87092450f6367"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/arthurpemberton.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/arthurpemberton.com\/#\/schema\/person\/e4504c0e198fc8188db87092450f6367","name":"Arthur Pemberton","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/23f8dddfae2b25e80c1bf8fd2c90661b8d2079f05446bcb0d0973a5a92570536?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/23f8dddfae2b25e80c1bf8fd2c90661b8d2079f05446bcb0d0973a5a92570536?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/23f8dddfae2b25e80c1bf8fd2c90661b8d2079f05446bcb0d0973a5a92570536?s=96&d=mm&r=g","caption":"Arthur Pemberton"},"logo":{"@id":"https:\/\/secure.gravatar.com\/avatar\/23f8dddfae2b25e80c1bf8fd2c90661b8d2079f05446bcb0d0973a5a92570536?s=96&d=mm&r=g"},"description":"A seasoned web applications developer with over seven years of professional experience designing, developing, and deploying end-to-end solutions; and over ten years administering websites.","sameAs":["http:\/\/arthurpemberton.com\/","https:\/\/www.linkedin.com\/in\/arthurpemberton"]}]}},"_links":{"self":[{"href":"https:\/\/arthurpemberton.com\/wp-json\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arthurpemberton.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arthurpemberton.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arthurpemberton.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arthurpemberton.com\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":4,"href":"https:\/\/arthurpemberton.com\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":451,"href":"https:\/\/arthurpemberton.com\/wp-json\/wp\/v2\/posts\/336\/revisions\/451"}],"wp:attachment":[{"href":"https:\/\/arthurpemberton.com\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arthurpemberton.com\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arthurpemberton.com\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}