Accessing the HTML Source of a WebElement Using Selenium WebDriver
When testing web applications, it is often necessary to access the HTML source of an element or its children. Selenium WebDriver provides methods for retrieving both the entire page source and the source of individual elements.
To obtain the HTML source of an element, use the get_attribute('innerHTML') method. This method returns the source of the element's content. For example:
from selenium import webdriver wd = webdriver.Firefox() elem = wd.find_element_by_css_selector('#my-id') # Get the HTML source of the element's content element_source = elem.get_attribute('innerHTML')
For the HTML source of the element and its children, use the get_attribute('outerHTML') method. This method returns the source with the current element.
Note that the innerHTML and outerHTML attributes are not part of the standard HTML specification. They are specific to web browsers and may not be supported by all browsers.
The above is the detailed content of How Do I Access the HTML Source of a WebElement in Selenium WebDriver?. For more information, please follow other related articles on the PHP Chinese website!