Home > Article > Backend Development > How to Retrieve the HTML Source of a WebElement in Selenium WebDriver with Python?
Retrieving the HTML Source of a WebElement in Selenium WebDriver with Python
Question:
How can you obtain the HTML source code specifically for a selected web element using Selenium WebDriver's Python bindings?
Answer:
To retrieve the HTML source of a web element, including its child elements, follow these steps:
Utilize the find_element_by_css_selector() method to locate the desired web element by CSS selector:
<code class="python">elem = wd.find_element_by_css_selector('#my-id')</code>
Access the HTML source of the web element using the get_attribute() method, passing in the 'innerHTML' attribute:
<code class="python">html_source = elem.get_attribute('innerHTML')</code>
This technique allows you to retrieve the HTML source of individual elements, enabling you to interact with specific components of the web page more effectively.
The above is the detailed content of How to Retrieve the HTML Source of a WebElement in Selenium WebDriver with Python?. For more information, please follow other related articles on the PHP Chinese website!