Accessing HTML Source of WebElements in Selenium WebDriver Using Python
In Selenium WebDriver, retrieving the HTML source of a web element allows you to manipulate its specific content, beyond the entire page source.
Get Element Inner HTML
To retrieve the HTML source of an element's content, use the innerHTML attribute:
<code class="python">from selenium import webdriver driver = webdriver.Firefox() element = driver.find_element_by_css_selector('#my-id') element_html = element.get_attribute('innerHTML')</code>
Get Element Outer HTML
For the HTML source of the element itself, use outerHTML:
<code class="python">element_html = element.get_attribute('outerHTML')</code>
Compatibility
This solution has been tested with the ChromeDriver and has been found compatible with Python, Java, C#, Ruby, JavaScript, and PHP.
The above is the detailed content of How Can I Retrieve the HTML Source of WebElements in Selenium WebDriver using Python?. For more information, please follow other related articles on the PHP Chinese website!