Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import selenium.webdriver.chrome.webdriver
import Image, time, os
def chrome_take_full_page_screenshot(browser, file):
global GLOBAL
browser.maximize_window()
time.sleep(0.2)
total_width = browser.execute_script("return document.body.offsetWidth")
total_height = browser.execute_script("return document.body.parentNode.scrollHeight")
viewport_width = browser.execute_script("return document.body.clientWidth")
viewport_height = browser.execute_script("return window.innerHeight")
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
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
cache_id = 1
for rectangle in rectangles:
if not previous is None:
browser.execute_script("window.scrollTo({0}, {1})".format(rectangle[0], rectangle[1]))
time.sleep(0.2)
file_name = "{0}scroll_{1}_part_{2}.png".format(GLOBAL['tmp_path'], cache_id, part)
browser.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])
stitched_image.paste(screenshot, offset)
del screenshot
os.remove(file_name)
part = part + 1
previous = rectangle
stitched_image.save(file)
chrome_driver = r'D:\sdk\Selenium\chromedriver_v2.12.exe'
browser = selenium.webdriver.chrome.webdriver.WebDriver(chrome_driver)
browser.get("http://www.pchome.com.tw")
chrome_take_full_page_screenshot(browser, 'd:\\test.jpg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.