Save a Web Page with Python Selenium


We can save a webpage with Selenium webdriver in Python. To save a page we shall first obtain the page source behind the webpage with the help of the page_source method.

We shall open a file with a particular encoding with the codecs.open method. The file has to be opened in the write mode represented by w and encoding type as utf−8. Then use the write method to write the content obtained from the page_source method.

Syntax

n = os.path.join("C:\Users\ghs6kor\Downloads\Test", "PageSave.html")
f = codecs.open(n, "w", "utf−8")
h = driver.page_source
f.write(h)

Let us make an attempt to save the below webpage −

Example

from selenium import webdriver
import codecs
#set chromedriver.exe path
driver = webdriver.Chrome(executable_path="C:\chromedriver.exe")
driver.implicitly_wait(0.5)
#maximize browser
driver.maximize_window()
#launch URL
driver.get("https://www.tutorialspoint.com/index.htm")
#get file path to save page
n=os.path.join("C:\Users\ghs6kor\Downloads\Test","Page.html")
#open file in write mode with encoding
f = codecs.open(n, "w", "utf−8")
#obtain page source
h = driver.page_source
#write page source content to file
file.write(h)
#close browser
driver.quit()

Output

On opening the Page.html file in a browser.

Updated on: 02-Feb-2021

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements