The key is to enable headless mode! There is no need to flash and no need to load the page twice.
Full working code:
URL = 'http://www.w3schools.com/js/default.asp' options = webdriver.ChromeOptions() options.headless = True driver = webdriver.Chrome(options=options) driver.get(URL) S = lambda X: driver.execute_script('return document.body.parentNode.scroll'+X) driver.set_window_size(S('Width'),S('Height')) # May need manual adjustment driver.find_element_by_tag_name('body').screenshot('web_screenshot.png') driver.quit()
This is pretty much the same code that @Acumenus posted with minor improvements.
Summary of my findings
I decided to publish this anyway, because I could not find an explanation of what happens when the headless mode is off (the browser is displayed) to take screenshots. As I tested (with Chrome WebDriver), if headless mode is enabled, the screenshot is saved as desired. However, if headless mode is disabled, the saved screen shot has approximately the correct width and height, but the result varies depending on the particular case. Usually the top of the page that is visible on the screen is saved, but the rest of the image is just white. There was also a case with an attempt to save this thread to Qaru using the above link; even the upper part was not preserved, which was interestingly now transparent, while the rest was still white. The last case that I noticed was only once with the specified W3Schools link; where there are no white parts, but the top of the page repeats to the end, including the title.
I hope that this will help many of those who, for some reason, do not get the expected result, since I did not see anyone explicitly explain the requirement of headless mode with such a simple approach. Only when I myself found a solution to this problem, I found a message by @ vc2279, which says that the headless browser window can be set to any size (which, apparently, is also true for the opposite case), although the solution in my post improves that that it does not require reopening the browser / driver or reloading the page.
Additional offers
If this doesnโt work for some pages, I suggest trying to add time.sleep(seconds) before getting the page size. Another case is if the page requires scrolling to the end in order to load additional content, which can be solved using the scheight method from this post :
scheight = .1 while scheight < 9.9: driver.execute_script("window.scrollTo(0, document.body.scrollHeight/%s);" % scheight) scheight += .01
Also note that for some pages, content may not be present in any of the top-level HTML tags, such as <html> or <body> , for example, YouTube uses the <ytd-app> . As a last note, I found one page that "returned" a screenshot with a horizontal scrollbar, the window size required manual adjustment, i.e. the image width needed to be increased by 18 pixels, for example: S('Width')+18 .