2011-08-31 21:44:11 8 Comments
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)?
Related Questions
Sponsored Content
6 Answered Questions
8 Answered Questions
[SOLVED] How do I find an element that contains specific text in Selenium Webdriver (Python)?
- 2012-09-07 18:20:06
- josh
- 373455 View
- 182 Score
- 8 Answer
- Tags: python selenium selenium-webdriver
32 Answered Questions
52 Answered Questions
[SOLVED] Take a screenshot with Selenium WebDriver
- 2010-08-06 08:52:18
- James Hollingworth
- 469466 View
- 446 Score
- 52 Answer
- Tags: selenium-webdriver screenshot
14 Answered Questions
3 Answered Questions
[SOLVED] Headless Browser and scraping - solutions
- 2013-08-30 18:38:58
- Inoperable
- 71496 View
- 347 Score
- 3 Answer
- Tags: selenium web-scraping scrapy phantomjs casperjs
4 Answered Questions
[SOLVED] How to get a color of a web element using Selenium WebDriver with python?
- 2013-02-27 00:54:51
- nids
- 28937 View
- 8 Score
- 4 Answer
- Tags: python selenium automation selenium-webdriver
3 Answered Questions
[SOLVED] Selenium - Error getting WebElement within iFrame
- 2017-09-05 12:37:48
- Vicente S.
- 75 View
- 0 Score
- 3 Answer
- Tags: html selenium iframe automated-tests
5 Answered Questions
[SOLVED] Selenium Webdriver and PageFactory initialize List<WebElement> elements
- 2011-11-04 10:50:51
- Patrick Magee
- 26772 View
- 5 Score
- 5 Answer
- Tags: webdriver selenium-webdriver
13 comments
@Rusty 2018-02-04 17:32:45
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.
@Rusty 2018-02-05 04:58:38
You can also use driver.find_element_by_tag("body") to reach the body content of the page.
@Nerijus 2011-12-20 12:49:48
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
.@bibstha 2012-03-22 13:53:46
innerHTML is a not DOM attribute. So above answer wouldn't work. innerHTML is a javascript javascript value. Doing above would return null. The answer by nilesh is the proper answer.
@Ryan Shillington 2012-07-10 02:04:43
This works great for me, and is much more elegant than the accepted answer. I'm using Selenium 2.24.1.
@CuongHuyTo 2012-07-23 10:57:50
Though innerHTML is not a DOM attribute, it is well supported by all major browsers (quirksmode.org/dom/w3c_html.html). It works also well for me.
@Kelvin 2012-08-20 19:45:01
+1 This appears to work in ruby also. I have a feeling that the
getAttribute
method (or equivalent in other languages) just calls the js method whose name is the arg. However the documentation doesn't explicitly say this, so nilesh's solution should be a fallback.@Andrew B. 2012-08-30 20:00:06
I'm getting this: content.get_attribute('innerHTML') == u'<div>...</div>'
@acdcjunior 2014-05-22 20:54:05
This fails for
HtmlUnitDriver
. Works forChromeDriver
,FirefoxDriver
,InternetExplorerDriver
(IE10) andPhantomJSDriver
(I haven't tested others).@Momer 2014-06-17 20:12:34
@acdcjunior - HtmlUnit's javascript support is pretty weak; I'd imagine by extension they haven't supported this. More info at this thread
@mvndaai 2014-11-07 20:36:42
In Ruby it
element.attribute("innerHTML")
if anyone needs it.@Bharat Mane 2016-04-26 12:43:43
nice- elem.get_attribute("innerHTML") It works by using this.
@ShaBANG 2016-10-10 19:36:42
in Node:
element.getAttribute('innerHTML')
@Shubham Jain 2017-09-03 07:18:46
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 :-
@oleksii.burdin 2011-09-07 14:23:30
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.
@Chris W. 2011-09-07 21:17:18
Python actually does have a "gettext" equivalent (I think its just the "text" attribute?) but that actually just returns the "plaintext" between HTML tags and won't actually return the full HTML source.
@Ryan Shillington 2012-07-10 02:06:18
This returns only the plain text (not the html) in Java too.
@HelloW 2013-09-12 18:17:19
you must reference it like you said elem[0] otherwise it doesn't work
@StanleyD 2013-07-09 14:18:56
If you are interested in a solution for Remote Control in Python, here is how to get innerHTML:
@Shane 2013-08-04 00:01:28
Thanks for the help, I have used this. I also find
innerHTML = {solenium selector code}.text
works just the same.@WltrRpo 2016-03-29 21:25:03
Java with Selenium 2.53.0
@Corey Goldberg 2017-05-31 02:18:00
that's not what the question asked for
@Stephan 2017-07-25 06:23:54
Depending on the webdriver, the
getPageSource
method may not return the actual page source (ie with possible javascript changements). The returned source may be the raw source sent by the server. The webdriver doc must be checked to ensure this point.@John Alberts 2013-04-15 20:59:33
In Ruby, using selenium-webdriver (2.32.1), there is a
page_source
method that contains the entire page source.@nilesh 2011-09-03 03:29:14
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.@Chris W. 2011-09-07 21:15:12
This is essentially what I ended up doing, albeit with the Python equivalent.
@Ryan Shillington 2012-07-10 02:05:28
I think the answer below, using element.getAttribute("innerHTML") is a lot easier to read. I don't understand why people are voting it down.
@Anthon 2014-04-30 08:15:40
No need to call javascript at all. In Python just use element.get_attribute('innerHTML')
@nilesh 2014-04-30 13:25:36
@Anthon
innerHTML
is not a DOM attribute. When I answered this question in 2011, it did not work for me, looks like now some browsers are supporting it. If it works for you then usinginnerHTML
is cleaner. However there is no guarantee it will work on all browsers.@Illidan 2015-06-06 14:29:10
Apparently, this is the only way to get innerHTML while using RemoteWebDriver
@Zorgijs 2014-05-30 10:25:21
And in PHPUnit selenium test it's like this:
@nefski 2014-03-06 14:52:17
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)
@Mark 2013-03-20 18:08:52
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.
@David 2013-08-22 11:47:07
this worked: elem.attribute("outerHTML") in ruby
@CodeGuru 2013-10-17 23:41:28
Can I set a delay and get the latest source? There are dynamic contents loaded using javascript.
@TheRookierLearner 2014-10-20 16:01:42
Does this work even if the page is not fully loaded? Also, is there any way to set a delay like @FlyingAtom mentioned?
@Tiffany G 2013-03-22 15:46:21
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)
.@Ilya 2012-08-31 04:04:51
This code really works to get JavaScript from source as well!