In Selenium WebDriver, use Python to obtain the WebElement's HTML source? Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 ElementThe 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 Comment More infoAdvertise with us Next Article Get all text of the page using Selenium in Python V vrinda_ahuja Follow Improve Article Tags : Software Testing Automation Testing Selenium Testing Tools Similar Reads How to use Selenium and Selenium WebDriver Manager to Login to a Website with Python? Selenium is an open-source tool that is used for automating the tests carried out on different web browsers. Selenium WebDriver is a web framework that allows executing cross-browsing testing. This article focuses on discussing how to log in to a website with Python using Selenium and Selenium WbDri 5 min read Selenium WebDriver - WebElement Commands Selenium is an open-source program that automates web browsers. Selenium Webdriver is mainly used to execute the scripts according to the browser we are using. Prerequisites1. Install the Selenium Python Package.Use the following command to install the Selenium Python package: pip install selenium 2 6 min read How to use xPath in Selenium WebDriver to grab SVG elements? Selenium WebDriver is a powerful tool for automating web browsers to test the functionality of web applications. However, when working with SVG elements on a webpage, it can be tricky to locate and interact with them using standard locators like ID, class, or name. In such cases, XPath comes to the 2 min read Wait Until Page Is Loaded With Selenium WebDriver For Python Selenium is an automation tool or a web framework used mainly in testing web applications across various browsers. Apart from testing web applications, we can also perform various tasks with selenium. With the help of selenium, we can also perform various web related tasks such as web scraping, web 5 min read Get all text of the page using Selenium in Python As we know Selenium is an automation tool through which we can automate browsers by writing some lines of code. It is compatible with all browsers, Operating systems, and also its program can be written in any programming language such as Python, Java, and many more. Selenium provides a convenient A 3 min read Interacting with Webpage - Selenium Python Seleniumâs Python module is designed for automating web testing tasks in Python. It provides a straightforward API through Selenium WebDriver, allowing you to write functional and acceptance tests. To open a webpage, you can use the get() method for navigation. However, the true power of Selenium li 4 min read Like