Home  >  Article  >  Backend Development  >  How to Retrieve the HTML Source of a WebElement in Selenium WebDriver with Python?

How to Retrieve the HTML Source of a WebElement in Selenium WebDriver with Python?

Linda Hamilton
Linda HamiltonOriginal
2024-11-04 22:52:02833browse

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:

  1. 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>
  2. 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn