In Selenium WebDriver, use Python to obtain the WebElement’s HTML source?

The article focuses on discussing how to use Python to obtain the WebElement’s HTML source.

Prerequisites:

Selenium Python Installation: Using Selenium Webdriver, one can obtain the HTML source of any WebElement.

What is an HTML Source?

HTML Source is described as the HTML code that underlies a certain web element on a web page.

  • It is the source of the HTML page which means it is the source code of the page that we visually see.
  • This HTML source can be actually useful if we are viewing a new page and we want to see how is it actually implemented.
  • It can also be used to view some hidden elements that might not be visually present on the webpage.

What is Web Element?

Web Element is anything that is displayed on the webpage.

  • Web Element is any HTML element present on the webpage. It can either be a form element like a text box, a button, a section, a heading, etc.
  • The Web Element can be recognized using various attributes like id, class name, link text, xpath, etc.
  • Web Element can be utilized in various ways using Selenium like for example, performing some action on the element (clicking a button), viewing other attributes of the element, finding the location of the element on the page and many more.

Steps to Retrieve the HTML Source of a Web Element

The source of the HTML Element can be found from the innerHTML attribute. The content between the opening tag and the ending tag is the value of the innerHTML attribute of a DOM element.

This is the method which is used in Python to get the innerHTML attribute:

element.get_attribute(‘innerHTML’)

The complete code for the program to extract the HTML source is the following:

Python




# Python program to retrieve HTML
# source code of the web element
from selenium import webdriver
import time
  
# string constants
PATH = r"C:\Users\VRINDA\Desktop\chromedriver_win32\chromedriver.exe"
URL = "https://vrii14.github.io/"
  
# chrome options
options = webdriver.ChromeOptions();
options.add_experimental_option('excludeSwitches', ['enable-logging']);
  
try:
    browser = webdriver.Chrome(PATH, options=options);
    browser.get(URL);
    browser.maximize_window();
    element = browser.find_element_by_id("contact");
    print(element.get_attribute('innerHTML'))
    time.sleep(6);
finally:
    browser.quit()


Ouput:

This is the output received on the console printing the innerHTML attribute of the element.

This is the part of the webpage of which the innerHTML is calculated :

Element of the webpage



Contact Us