How to get Div HTML in Selenium Python?

Selenium Python – Get Div HTML

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

To get the outer HTML of a div element in Selenium, locate and get the div element, call the get_attribute() method on the div element and pass "outerHTML" as argument. The method with the specified argument returns the outer HTML of the div element.

The following code snippet returns the outer HTML of the div element mydiv.

mydiv.get_attribute("outerHTML")

Examples

In the following examples, we shall consider loading the following HTML file via the driver object.

index.html

<html>
  <body>
    <div id="mydiv1">Apple is red.</div>
    <div id="mydiv2">Banana is yellow.</div>
    <div id="mydiv3">Cherry is red.</div>
  </body>
</html>

1. Get the outer HTML of div element whose id=”mydiv2″

In the following program, we initialize a webdriver, navigate to a specific URL (index.html), get the div element whose id="mydiv2", get its outer HTML using get_attribute() method, and print it to standard output.

Python Program

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

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

# Navigate to the url
driver.get('http://127.0.0.1:5500/index.html')

# Get the div element by ID
try:
    div_element = driver.find_element(By.ID, "mydiv2")
    # Get outer HTML of div element
    div_outer_html = div_element.get_attribute("outerHTML")
    print(div_outer_html)
except NoSuchElementException:
    print("The div element does not exist.")

# Close the driver
driver.quit()

Output

Selenium Python - Get the outer HTML of a div element

2. Get the outer HTML of all div elements

In the following program, we iterate over the div elements in the page, and print the outer HTML of each div.

Python Program

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

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

# Navigate to the url
driver.get('http://127.0.0.1:5500/index.html')

# Find all div elements
div_elements = driver.find_elements(By.TAG_NAME, "div")

for index, div_element in enumerate(div_elements):
    print(f"\nDiv {index + 1}")
    # Get outer HTML of div element
    div_outer_html = div_element.get_attribute("outerHTML")
    print(div_outer_html)

# Close the driver
driver.quit()

Output

Selenium Python - Get the outer HTML of all div elements

Summary

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

⫷ Previous tutorialNext tutorial ⫸

Quiz on Selenium

Q1. What is Selenium Python used for?

Not answered

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

Not answered

Q3. What is the method used to navigate back to the previous page in Selenium Python?

Not answered

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

Not answered

Q5. What is the method used to clear the text from an input field in Selenium Python?

Not answered