How to get the HTML of an element in Selenium Python?

Selenium Python – Get the HTML of an element

In this tutorial, you will learn how to get the HTML (outer HTML) of an element using Selenium in Python.

To get the HTML (outer HTML) of an element in Selenium Python, find the element, and then call get_attribute() method of the element object and pass "outerHTML" as argument. The get_attribute() method returns the HTML of the element.

The following code snippet returns the HTML of the element object.

element.get_attribute("outerHTML")

The element is of type WebElement.

Examples

In the following example, we shall consider loading the HTML file at path https://pythonexamples.org/tmp/selenium/index-10.html . The contents of this HTML file is given below.

<html>
  <body>
    <h2>Hello User!</h2>
    <p id="msg">This is my <strong>first</strong> paragraph. This is a sample text. Welcome!</p>
    <div id="mydiv">
      <div class="child">Child 1</div>
      <div class="child">Child 2</div>
    </div>
  </body>
</html>
Copy

1. Get the HTML of element with id=”msg”

In the following program, we initialize a webdriver, navigate to a specific URL, get the (outer) HTML of the element with id="msg", and print the outer HTML to the standard output.

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-10.html')

# Get the element
element = driver.find_element(By.ID, "msg")

# Get HTML of the element
html = element.get_attribute("outerHTML")
print(html)

# Close the driver
driver.quit()
Copy

Output

Selenium Python - Example for getting outer HTML of a paragraph element

2. Get the outer HTML of div element with id=”mydiv”

In this example, we are going to get the outer HTML of a div element whose id="mydiv".

Python Program

from selenium import webdriver
from selenium.webdriver.chrome.service import Service as ChromeService
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.by import By

# Setup chrome driver
driver = webdriver.Chrome(service=ChromeService(ChromeDriverManager().install()))

# Navigate to the url
driver.get('https://pythonexamples.org/tmp/selenium/index-10.html')

# Get the element
element = driver.find_element(By.XPATH, '//div[@id="mydiv"]')

# Get HTML of the element
html = element.get_attribute("outerHTML")
print(html)

# Close the driver
driver.quit()
Copy

Output

Selenium Python - Example for getting outer HTML of a div element

Summary

In this Python Selenium tutorial, we have given instructions on how to get the outer HTML of an element using get_attribute() method of WebElement class, with example programs.

Related Tutorials

Quiz on Selenium

Q1. What is Selenium Python used for?

Not answered

Q2. Which of the following is not a popular web driver used in Selenium Python?

Not answered

Q3. What is the method used to interact with web elements in Selenium Python?

Not answered

Q4. What is the method used to switch to a different frame in Selenium Python?

Not answered

Q5. What is the method used to select a value from a drop-down list in Selenium Python?

Not answered
Code copied to clipboard successfully 👍