Get the HTML Source of a WebElement Using Selenium WebDriver in Python
When working with Selenium WebDriver in Python, you can retrieve the entire page source using wd.page_source. However, to obtain the HTML source of a specific element (and its children), there's a different approach.
The desired functionality is not explicitly documented for Python. However, you can access an element's HTML source using the get_attribute() method with the 'innerHTML' or 'outerHTML' attribute.
innerHTML
The 'innerHTML' attribute returns the HTML source of the element's content:
<code class="python">element_source = elem.get_attribute('innerHTML')</code>
outerHTML
The 'outerHTML' attribute returns the HTML source of the entire element, including its child elements:
<code class="python">element_source = elem.get_attribute('outerHTML')</code>
This approach has been tested and confirmed to work with the ChromeDriver.
The above is the detailed content of How to Get the HTML Source of a Specific WebElement Using Selenium WebDriver in Python?. For more information, please follow other related articles on the PHP Chinese website!