Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (19.7k points)

I'm using the Python bindings to run Selenium WebDriver.

from selenium import webdriver

wd = webdriver.Firefox()

 I know I can grab a web element 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 any way to get the "element source"?

elem.source   # <-- returns the HTML as a string

The selenium web driver 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)?

 

1 Answer

0 votes
by (62.9k points)

You can read the innerHTML attribute to get a 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.

Hope this helps!

If you are interested to learn Selenium on a much deeper level and want to become a professional in the testing domain, check out Intellipaat’s automation testing training!

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.4k questions

32.5k answers

500 comments

108k users

Browse Categories

...