I'm using the Python bindings to run Selenium WebDriver.
from selenium import webdriver
wd = webdriver.Firefox()
I know I can grab a webelement like so...
elem = wd.find_element_by_css_selector('#my-id')
And I know I can get the full page source with...
wd.page_source
But is there anyway to get the "element source"?
elem.source # <-- returns the HTML as a string
The selenium webdriver docs for Python are basically non-existent and I don't see anything in the code that seems to enable that functionality.
Any thoughts on the best way to access the HTML of an element (and its children)?
Using the attribute method is, in fact, easier and more straight forward.
Using Ruby with the Selenium and PageObject gems, to get the class associated with a certain element, the line would be
element.attribute(Class)
.The same concept applies if you wanted to get other attributes tied to the element. For example, if I wanted the String of an element,
element.attribute(String)
.Sure we can get all HTML source code with this script below in Selenium Python:
If you you want to save it to file:
I suggest saving to a file because source code is very very long.
InnerHTML will return element inside the selected element and outerHTML will return inside HTML along with the element you have selected
Example :- Now suppose your Element is as below
innerHTML element Output
outerHTML element Output
Live Example :-
http://www.java2s.com/Tutorials/JavascriptDemo/f/find_out_the_difference_between_innerhtml_and_outerhtml_in_javascript_example.htm
Below you will find the syntax which require as per different binding. Change the
innerHTML
toouterHTML
as per required.Python:
Java:
If you want whole page HTML use below code :-
You can read
innerHTML
attribute to get source of the content of the element orouterHTML
for source with the current element.Python:
Java:
C#:
Ruby:
JS:
PHP:
Tested and works with the
ChromeDriver
.Looks outdated, but let it be here anyway. The correct way to do it in your case:
or
Both are working for me (selenium-server-standalone-2.35.0)
Java with Selenium 2.53.0