Get Page Source or HTML Source of Web Page

To get the page source or HTML source of a web page using Selenium in Java, call getPageSource() on the Web Driver object.

The following is a simple code snippet to get the page source of current web page.

driver.getPageSource()

getPageSource() returns a String representing the content of the last loaded page. If any changes are made to the web-page via JavaScript, or such, it depends on the specific web driver if getPageSource() returns the modified content or not.

ADVERTISEMENT

Example

In the following program, we write Selenium Java code to visit WikiPedia Main Page, and get the page source of this web page.

Java Program

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class MyAppTest {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");  
    	WebDriver driver = new ChromeDriver();
        driver.get("https://en.wikipedia.org/wiki/Main_Page");

        String pageSource = driver.getPageSource();
        System.out.println(pageSource);	
        
        driver.quit();
    }
}

Screenshot

Get Page Source of Web Page - Selenium