How can I retrieve the HTML source of a specific WebElement in Selenium WebDriver using Python?

147    Asked by JakeSanderson in QA Testing , Asked on Jan 15, 2025

I am using Python with Selenium WebDriver and can perform basic operations like locating elements and fetching the full page source.

To retrieve the HTML source of a specific WebElement (including its children) in Selenium WebDriver using Python, you can use the get_attribute('outerHTML') method. Here's how you can achieve it:


Solution

from selenium import webdriver
# Set up the WebDriver
driver = webdriver.Firefox() # You can use any browser WebDriver
driver.get("https://example.com") # Replace with your target URL
# Locate the element
element = driver.find_element_by_css_selector("#my-id") # Replace with your desired CSS selector
# Get the HTML source of the element
element_html = element.get_attribute("outerHTML")
print(element_html)
# Close the browser
driver.quit()

Explanation

Locate the Element:

Use methods like find_element_by_css_selector, find_element_by_id, or find_element (with By locators) to locate the desired WebElement.

Retrieve HTML Source:

outerHTML: Returns the HTML of the element including the element itself and all its children.

innerHTML: Returns the HTML inside the element, excluding the element itself.

Example:

If the HTML is:


    Hello World

outerHTML will return:


    Hello World

innerHTML will return:

  Hello World

Print or Process the HTML:

The get_attribute("outerHTML") method returns the HTML as a string, which you can print or further process in your script.

Notes

  • Replace #my-id with the actual CSS selector or XPath of the element you want to target.

If you're using Selenium 4, prefer find_element with By locators:

from selenium.webdriver.common.by import By
element = driver.find_element(By.CSS_SELECTOR, "#my-id")


Your Answer

Answers (23)

Your website is a true example of how great content should be delivered. The design is user-friendly, and every detail feels well-thought-out. I admire how your posts are filled with valuable information yet remain easy to understand. It’s always a pleasure to learn something new here. Your dedication to sharing quality content inspires me to think about how I can better engage my audience. Please keep sharing your amazing insights because they’re a big help to readers like me. Thank you for all your hard work and creativity!


website


1 Week

Wow, this post is amazing! Your insights on travel hacks are so practical and helpful. I’ve been looking for ways to make my trips more enjoyable, and your tips are spot on. I’ll definitely try out the packing tricks you mentioned. Thanks for sharing such valuable information.

View data


1 Week

’d really love to be a part of online community where I can get responses from other knowledgeable individuals that share the same interest. If you have any recommendations, please let me know. Appreciate it! Feel free to visit my website;

best cryptocurrency miner



1 Week

Interviews

Parent Categories