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,330 points
33,050 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,890 points
+1 vote

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

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

Related Questions In Selenium

0 votes
1 answer

How to get the HTML source of a webpage using Selenium in Java?

There is a method called getPageSource() in ...READ MORE

answered Jan 10, 2019 in Selenium by Sneha
11,649 views
0 votes
1 answer
0 votes
2 answers

How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver ?

JavascriptExecutor js = (JavascriptExecutor) driver; js.executeScript("window.scrollBy("X", " y ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
1,667 views
0 votes
2 answers

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,510 points
3,531 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31 in Selenium by Sri
• 2,970 points
4,538 views
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,890 points
2,596 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 2 in Selenium by Sri
• 2,970 points
2,864 views
+1 vote
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,890 points
6,373 views
0 votes
2 answers

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

Hi , we have inbuilt method Maximize(). driver.Manage().Wind ...READ MORE

answered Sep 5 in Selenium by Sri
• 2,970 points
9,467 views