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条回答
其实,你不懂
2楼-- · 2019-01-01 08:10

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

查看更多
弹指情弦暗扣
3楼-- · 2019-01-01 08:13

Sure we can get all HTML source code with this script below in Selenium Python:

elem = driver.find_element_by_xpath("//*")
source_code = elem.get_attribute("outerHTML")

If you you want to save it to file:

f = open('c:/html_source_code.html', 'w')
f.write(source_code.encode('utf-8'))
f.close()

I suggest saving to a file because source code is very very long.

查看更多
明月照影归
4楼-- · 2019-01-01 08:13

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

<tr id="myRow"><td>A</td><td>B</td></tr>

innerHTML element Output

<td>A</td><td>B</td>

outerHTML element Output

<tr id="myRow"><td>A</td><td>B</td></tr>

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 to outerHTML as per required.

Python:

element.get_attribute('innerHTML')

Java:

elem.getAttribute("innerHTML");

If you want whole page HTML use below code :-

driver.getPageSource();
查看更多
刘海飞了
5楼-- · 2019-01-01 08:15

You can read innerHTML attribute to get source of the content of the element or outerHTML for source with the current element.

Python:

element.get_attribute('innerHTML')

Java:

elem.getAttribute("innerHTML");

C#:

element.GetAttribute("innerHTML");

Ruby:

element.attribute("innerHTML")

JS:

element.getAttribute('innerHTML');

PHP:

$elem.getAttribute('innerHTML');

Tested and works with the ChromeDriver.

查看更多
谁念西风独自凉
6楼-- · 2019-01-01 08:16

Looks outdated, but let it be here anyway. The correct way to do it in your case:

elem = wd.find_element_by_css_selector('#my-id')
html = wd.execute_script("return arguments[0].innerHTML;", elem)

or

html = elem.get_attribute('innerHTML')

Both are working for me (selenium-server-standalone-2.35.0)

查看更多
无与为乐者.
7楼-- · 2019-01-01 08:18

Java with Selenium 2.53.0

driver.getPageSource();
查看更多
登录 后发表回答