How to get HTML Source of WebElement in Selenium WebDriver using Python?

0 votes

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

from selenium import webdriver
wd = webdriver.Firefox()

I’m getting web element

elem = wd.find_element_by_css_selector('#my-id')

I’m getting the full page source with...

wd.page_source

But what are other ways to get the "element source"?

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

How to access the HTML of an element (and its children)?

Jun 13, 2018 in Selenium by Martin
• 4,220 points
15,616 views

2 answers to this question.

+1 vote

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

answered Jun 13, 2018 by Samarpit
• 5,130 points
0 votes

If you want to get the html of the element in python use:

ele.get_atrribute("outerHTML")
answered May 31 by Atul Singh

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
770 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
1,014 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
719 views
0 votes
1 answer

How to handle notifications in Python with Selenium (Chrome WebDriver)

Below will help you: You can disable the ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
2,481 views
0 votes
1 answer

How to open a browser window in full screen using Selenium WebDriver with C#

Simply Use this: driver.Manage().Window.Maximize(); READ MORE

answered Jun 7, 2018 in Selenium by Samarpit
• 5,130 points
3,776 views