How to get Page Source in Selenium (WebDriver) using Java?

Hi,

I am working on Selenium test cases for testing the web application. Now I have to get the source of the web page.

How to get Page Source in Selenium (WebDriver) using Java?

Thanks

View Answers

October 14, 2016 at 7:48 AM

Hi,

You can use the following code to achieve this:

String pageSource = driver.getPageSource();

Here is the full source code:

package net.roseindia;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.firefox.FirefoxDriver;

public class TestLoginApp {

    public static void main(String[] args) {
        WebDriver driver;
        System.setProperty("webdriver.gecko.driver", "C:\\DeepakData\\selenium\\geckodriver.exe");
        driver = new FirefoxDriver();
        driver.get("http://www.roseindia.net");

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

}

Thanks









Related Tutorials/Questions & Answers:
Advertisements