Last active
October 16, 2016 23:43
-
-
Save mattplacer/42c87ff7cf78d84bb86f to your computer and use it in GitHub Desktop.
Quickie workaround for Chrome+WebDriver full-page screenshots
This file contains 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
def chrome_screenshot(self, filename): | |
h = 1080 | |
w = 1920 | |
d = self.driver | |
p_w = int(d.execute_script("return document.body.offsetWidth")) | |
p_h = int(d.execute_script("return document.body.parentNode.scrollHeight")) | |
b_w = int(d.execute_script("return window.innerWidth||document.documentElement.clientWidth||document.body.clientWidth||0;")) | |
b_h = int(d.execute_script("return window.innerHeight||document.documentElement.clientHeight||document.body.clientHeight||0;")) | |
c_w = w - p_w | |
c_h = h - b_h | |
d.set_window_size(p_w+c_w, p_h+c_h) | |
time.sleep(0.2) | |
ret = d.get_screenshot_as_file(filename) | |
d.set_window_size(w,h) | |
time.sleep(0.2) | |
return ret |
Note that one place this can fall down is if the page you're trying to capture has an area with its height set to a proportion of the viewport. I don't encounter this very often but you may.
(The real fix is of course for Chrome to just do this correctly, but it is clear that that is never going to happen!)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This assumes the webdriver driver has been set up and is available as self.driver.
It's an alternative to this: https://snipt.net/restrada/python-selenium-workaround-for-full-page-screenshot-using-chromedriver-2x/
it's probably going to break on something, but unlike the scroll-around-the-page method it doesn't run in to trouble with things like navbars that are glued to the top of the window.