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)?
The method to get the rendered HTML I prefer is following:
However the above method removes all the tags( yes the nested tags as well ) and returns only text content. If you interested in getting the HTML markup as well, then use the method below.
In Ruby, using selenium-webdriver (2.32.1), there is a
page_source
method that contains the entire page source.If you are interested in a solution for Remote Control in Python, here is how to get innerHTML:
There is not really a straight-forward way of getting the html source code of a webelement. You will have to use JS. I am not too sure about python bindings but you can easily do like this in Java. I am sure there must be something similar to
JavascriptExecutor
class in Python.I hope this could help: http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html
Here is described Java method:
But unfortunately it's not available in Python. So you can translate the method names to Python from Java and try another logic using present methods without getting the whole page source...
E.g.
This code really works to get JavaScript from source as well!