user2186465
user2186465

Reputation: 197

Java selenium getPageSource not working

I need the source of the url given in the program. But the program returns oly some json data not the entire page source. What's the problem??

public class selenium
{
public static void main(String[] args)
{
    selenium.loadPage("http://photos.filmibeat.com/celebs/kajal-aggarwal/photos-c14-e13421-p592995.html");
}
public static void loadPage(String url)
{

    WebDriver driver = new FirefoxDriver();

    driver.get(url);

    String html = driver.getPageSource();

    System.out.println(html);

    driver.quit();        

}
}

Upvotes: 1

Views: 2563

Answers (3)

Vicky
Vicky

Reputation: 3021

I am just adding more info on @alecxe answer The solution provided by alecxe is working perfectly

The console output size of eclipse is by default only 80000 characters

enter image description here

Window > Preferences, go to the Run/Debug > Console section > then disable limit console option

or write data into a file

    File file = new File("path/filename.txt");
    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(content);
    bw.close();

Hope this helps you

Upvotes: 2

Hari kishen
Hari kishen

Reputation: 473

The above problem can be handled by both Implicit and Explicit wait. Here i tried with Implicit wait with your code. Please try this. It worked for me with the below code.

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Pagesource
{
public static void main(String[] args)
{
    Pagesource.loadPage("http://photos.filmibeat.com/celebs/kajal-aggarwal/photos-c14-e13421-p592995.html");
}
public static void loadPage(String url)
{
    WebDriver driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get(url);

    String html = driver.getPageSource();

    System.out.println(html);

    driver.quit();      
}

Upvotes: 2

alecxe
alecxe

Reputation: 473873

The problem is that you are getting the page source too early - the page is not yet loaded at that moment. Use an Explicit Wait to wait for a particular element on a page to become visible.

For instance, waiting for the photo list block to become visible:

WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("photoListBlock"));

Upvotes: 2

Related Questions