Created
January 14, 2017 20:51
-
-
Save scuerda/2e6d109e392027709bfaa0d8c5f46be5 to your computer and use it in GitHub Desktop.
Capture full screen image of webpage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from selenium import webdriver | |
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities | |
def setup_phantom_browser(): | |
user_agent = ( | |
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.1 Safari/537.36") | |
dcap = dict(DesiredCapabilities.PHANTOMJS) | |
dcap["phantomjs.page.settings.userAgent"] = user_agent | |
browser = webdriver.PhantomJS(executable_path='/usr/local/lib/node_modules/phantomjs/lib/phantom/bin/phantomjs', | |
desired_capabilities=dcap, | |
service_log_path='logs/phantom.log', | |
service_args=["--webdriver-loglevel=SEVERE"] | |
) | |
browser.set_window_size(1400, 1000) | |
return browser | |
def get_screenshot(url, filename): | |
b = setup_phantom_browser() | |
b.get(url) | |
width = b.execute_script("return Math.max(document.body.scrollWidth, document.body.offsetWidth, " | |
"document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth);") | |
height = b.execute_script("return Math.max(document.body.scrollHeight, document.body.offsetHeight, " | |
"document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight);") | |
b.set_window_size(width, height) | |
b.save_screenshot(filename) | |
b.quit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment