How to get innerHTML of whole page in selenium driver?


We can get the innerHTML of the whole page in Selenium. We shall use the method getPageSource and print the values captured by it in the console.

Syntax

String s = driver.getPageSource();

We can also get the HTML source code via Javascript commands in Selenium. We shall utilize executeScript method and pass the command return document.body.innerHTML as a parameter to the method.

Syntax

JavascriptExecutor j = (JavascriptExecutor) driver;
String s = (String) j.executeScript("return document.body.innerHTML;");

Example

Code Implementation with getPageSource.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
public class PageHTMLcode{
   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);
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      // getPageSource method to obtain HTML of page
      System.out.println("Get HTML of page: "+ driver.getPageSource());
      driver.quit();
   }
}

Code Implementation with Javascript Executor.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.JavascriptExecutor;
public class PageHTMLcodeJS{
   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);
      String url = "https://www.tutorialspoint.com/index.htm";
      driver.get(url);
      // Javascript executor to obtain page HTML
      JavascriptExecutor j = (JavascriptExecutor) driver;
      String s = (String) j.executeScript("return document.body.innerHTML;");
      System.out.println("Get HTML of page: "+ s);
      driver.quit();
   }
}

Updated on: 28-Dec-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements