Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Get contents of entire page using Selenium

  • Last Updated : 25 Feb, 2021

In this article, we will discuss ways to get the contents of the entire page using Selenium. There can broadly be two methods for the same. Let’s discuss them in detail.

Method 1:

For extracting the visible text from the entire page, we can use the find_element_by_* methods which help us find or locate the elements on the page. Then, We will use the text method which helps to retrieve the text from a specific web element.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course


  • Import module
  • Instantiate driver
  • Get content of the page
  • Display contents scraped
  • Close driver



To find or locate multiple elements on a page:

  • find_element_by_link_text
  • find_element_by_partial_link_text
  • find_element_by_xpath
  • find_element_by_tag_name
  • find_element_by_class_name
  • find_element_by_css_selector
  • find_element_by_id
  • find_element_by_name

We can use these above methods for finding or locating elements on a entire page. Most commonly used method is find_element_by_xpath which helps us to easily locate any elements. We will use appropriate methods as per our requirement.



# importing the modules
from selenium import webdriver
from import ChromeDriverManager
# using webdriver for chrome browser
driver = webdriver.Chrome(ChromeDriverManager().install())
# using target url
# printing the content of entire page
# closing the driver


Method 2:

There is one another method available for achieving our desired output. This one line will retrieve the entire text of the web page. Once we get the extracted data, with the help of file system, we will store the result inside the result.html file.


  • Import module
  • Instantiate webdriver
  • Get contents from the URL
  • Open a file
  • Save contents to a file
  • Close file
  • Close driver





# Importing important library
from selenium import webdriver
from import ChromeDriverManager
# using chrome browser
driver = webdriver.Chrome(ChromeDriverManager().install())
# Target url
# Storing the page source in page variable
page = driver.page_source.encode('utf-8')
# print(page)
# open result.html
file_ = open('result.html', 'wb')
# Write the entire page content in result.html
# Closing the file
# Closing the driver


Click here to download the output file of above program.

My Personal Notes arrow_drop_up
Recommended Articles
Page :