- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to get HTML code of a WebElement in Selenium?
We can get the html code of a webelement with the help of Selenium webdriver. We can obtain the innerHTML attribute to get the HTML content of the web element.
The innerHTML is an attribute of a webelement which is equal to the content that is present between the starting and ending tag. The getAttribute method is used for this and innerHTML is passed as an argument to the method.
Syntax
String s = element.getAttribute('innerHTML');
Let us see the below html code of an element. The innerHTML of the element shall be < You are browsing the best resource for <b>Online Education</b>.
Example
Code Implementation
import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.By; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebElement; public class HtmlCodeElement{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); driver.get("https://www.tutorialspoint.com/index.htm"); // identify element WebElement l=driver.findElement(By.cssSelector("h4")); // obtain the innerHTML with getAttribute method String s = l.getAttribute("innerHTML"); System.out.println("HTML code of element: " +s); driver.close(); } }
Output
- Related Articles
- Get HTML Source of WebElement in Selenium WebDriver using Python.
- How to get text from Selenium element WebElement object?
- How to extract the text of a webelement in Selenium?
- How to fetch values from a webelement in Selenium with python?
- How to scroll a Web Page using coordinates of a WebElement in Selenium WebDriver?
- How to get HTTP Response Code using Selenium WebDriver?
- How to get Response Status Code with Selenium WebDriver?
- Can I set any of the attribute value of a WebElement in Selenium?
- Accessing HTML source code using Python Selenium.
- How to get text from each cell of an HTML table using Selenium?
- How to get html with javascript rendered sourcecode by using Selenium?
- Can we get the HTTP Response Code in Selenium with Java?
- How to embed a video using HTML code?
- How do I get a parent HTML Tag with Selenium WebDriver using Java?
- How to specify "ENTER" button functionality in Selenium WebDriver code?

Advertisements