 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- 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 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();    } }Advertisements
 