How can I retrieve the HTML source of a specific WebElement in Selenium WebDriver using Python?
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")