Get HTML Source of WebElement in Selenium WebDrive

2019-01-01 07:51发布

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)?

13条回答
伤终究还是伤i
2楼-- · 2019-01-01 08:21

The method to get the rendered HTML I prefer is following:

driver.get("http://www.google.com")
body_html = driver.find_element_by_xpath("/html/body")
print body_html.text

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.

print body_html.getAttribute("innerHTML")
查看更多
零度萤火
3楼-- · 2019-01-01 08:27

In Ruby, using selenium-webdriver (2.32.1), there is a page_source method that contains the entire page source.

查看更多
何处买醉
4楼-- · 2019-01-01 08:32

If you are interested in a solution for Remote Control in Python, here is how to get innerHTML:

innerHTML = sel.get_eval("window.document.getElementById('prodid').innerHTML")
查看更多
笑指拈花
5楼-- · 2019-01-01 08:33

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.

 WebElement element = driver.findElement(By.id("foo"));
 String contents = (String)((JavascriptExecutor)driver).executeScript("return arguments[0].innerHTML;", element); 
查看更多
与风俱净
6楼-- · 2019-01-01 08:35

I hope this could help: http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html

Here is described Java method:

java.lang.String    getText() 

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.

 my_id = elem[0].get_attribute('my-id')
查看更多
倾城一夜雪
7楼-- · 2019-01-01 08:35
WebElement element = driver.findElement(By.id("foo"));
String contents = (String)((JavascriptExecutor)driver).executeScript("return      arguments[0].innerHTML;", element); 

This code really works to get JavaScript from source as well!

查看更多
登录 后发表回答