Java selenium getPageSource not working

不问归期 提交于 2021-01-28 19:39:16

问题


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();        

}
}

回答1:


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




回答2:


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"));



回答3:


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();      
}


来源:https://stackoverflow.com/questions/31307541/java-selenium-getpagesource-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!