Take Screenshot of Full Page with Selenium Python with Chromedriver

Take screenshot of full page with Selenium Python with chromedriver

How it works: set browser height as longest as you can...

#coding=utf-8
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

def test_fullpage_screenshot(self):
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--start-maximized')
driver = webdriver.Chrome(chrome_options=chrome_options)
driver.get("yoururlxxx")
time.sleep(2)

#the element with longest height on page
ele=driver.find_element("xpath", '//div[@class="react-grid-layout layout"]')
total_height = ele.size["height"]+1000

driver.set_window_size(1920, total_height) #the trick
time.sleep(2)
driver.save_screenshot("screenshot1.png")
driver.quit()

if __name__ == "__main__":
test_fullpage_screenshot()

Selenium does not take the screenshot of the whole website, when its not headless

You can use Screenshot_Clipping in order to scroll the page and take screenshot from each scroll.

Just run this command in python3

pip install Selenium-Screenshot

Then create an object of Screenshot:

ob=Screenshot_Clipping.Screenshot()

With that object you can use ob.full_Screenshot in order to capture full screen

Checkout PythonFullPageScreenShot project on Github for full source code

Note that screenshot can take only 10000 of height of website, You can scale to 100 in order to capture full height

Take screenshot of full web page with Selenium Python with Chrome driver

Here is what I did to get the full-size screenshot of a webpage

from selenium import webdriver
browser = webdriver.Chrome()

# We go to the webpage here and wait 2 seconds for fully load
browser.get('https://someURL')
time.sleep(2)

# Where to save the picture
directory = '/home'
# Video title
title = 'Hello world'

try:
# We try to get the top-level component in which all out page is its children
# This is different for each website
elem = browser.find_element_by_id('main-layout-content')
# Get the height of the element, and adding some height just to be sage
total_height = elem.size['height'] + 1000
# Set the window size - what is the size of our screenshot
# The width is hardcoded because the screensize is fixed for each computer
browser.set_window_size(1920, total_height)
# Wait for 2 seconds
time.sleep(2)
# Take the screenshot
browser.find_element_by_id(
'main-layout-content').screenshot(f'./{directory}/{title}.png')
except SystemError as err:
print('Take screenshot error at' + title)


Related Topics



Leave a reply



Submit