Take screenshot of full page with Selenium Python with chromedriver.


We can take a screenshot of a full page with Selenium webdriver in Python with chromedriver. First of all, we shall obtain the original window size with the get_window_size method.

Then with the help of JavaScript Executor we shall fetch the complete height and width of the page which is opened on the browser. Then set the window size to that dimension with the set_window_size method.

Next, capture the screenshot of the entire content within the body tag in the html with the screenshot method. This method accepts the path of the screenshot that will be captured as a parameter.

Example

from selenium import webdriver
#set chromodriver.exe path
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.maximize_window()
#launch URL
driver.get("https://www.tutorialspoint.com/index.htm")
#get window size
s = driver.get_window_size()
#obtain browser height and width
w = driver.execute_script('return document.body.parentNode.scrollWidth')
h = driver.execute_script('return document.body.parentNode.scrollHeight')
#set to new window size
driver.set_window_size(w, h)
#obtain screenshot of page within body tag
driver.find_element_by_tag_name('body').screenshot("tutorialspoint.png")
driver.set_window_size(s['width'], s['height'])
driver.quit()

Output

A new file called tutorialspoint.png gets created in the project folder.

Right−click on it and then select Properties. The Properties for pop−up comes up. Copy the location field.

Open it in a browser.

Updated on: 30-Jan-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements