Open In App

Get contents of entire page using Selenium

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.



Approach

Syntax:



driver.find_element_by_class_xpath(“/html/body”).text

To find or locate multiple elements on a page:

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.

Program:




# importing the modules
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
  
# using webdriver for chrome browser
driver = webdriver.Chrome(ChromeDriverManager().install())
  
# using target url
driver.get(
  
# printing the content of entire page
print(driver.find_element_by_xpath("/html/body").text)
  
# closing the driver
driver.close()

Output:

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.

Approach:

Syntax:

driver.page_source

Program:




# Importing important library
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
  
# using chrome browser
driver = webdriver.Chrome(ChromeDriverManager().install())
  
# Target url
driver.get(
  
# 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
file_.write(page)
  
# Closing the file
file_.close()
  
# Closing the driver
driver.close()

Output:

Click here to download the output file of above program.


Article Tags :