- Trending Categories
- Data Structure
- Operating System
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
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 −
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()
On opening the Page.html file in a browser.
- Smart Ways to Save a Web Page Forever!!
- How to extract text from a web page using Selenium and save it as a text file?
- How can I scroll a web page using selenium webdriver in python?
- Modern Web Automation With Python and Selenium
- Best way to take screenshot of a web page into Selenium?
- Take screenshot of full page with Selenium Python with chromedriver.
- Center pagination on a web page with CSS
- How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver?
- How to check a checkbox in a page in Selenium with python?
- Retrofit existing web page with mobile CSS
- What to press ctrl +f on a page in Selenium with python?
- What to press ctrl +c on a page in Selenium with python?
- How to get text with selenium web driver in python?
- Wait until page is loaded with Selenium WebDriver for Python.
- How to select a radio button in a page in Selenium with python?