【问题标题】:Java selenium getPageSource not workingJava selenium getPageSource 不起作用
【发布时间】:2015-09-27 05:47:47
【问题描述】:

我需要程序中给出的 url 的来源。但是程序只返回一些 json 数据而不是整个页面源。有什么问题??

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

}
}

【问题讨论】:

    标签: java selenium selenium-firefoxdriver


    【解决方案1】:

    我只是在@alecxe 答案上添加更多信息 alecxe 提供的解决方案运行良好

    eclipse的控制台输出大小默认只有80000个字符

    Window > Preferences,转到Run/Debug > Console section > 然后禁用limit console option

    或将数据写入文件

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

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      隐式显式等待都可以处理上述问题。 在这里,我尝试使用您的代码进行隐式等待。请试试这个。它对我有用下面的代码。

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

      【讨论】:

        【解决方案3】:

        问题是您获取页面源太早了 - 此时页面尚未加载。使用Explicit Wait 等待页面上的特定元素变为可见。

        例如,等待照片列表块变得可见:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-07-20
          • 2015-10-07
          相关资源
          最近更新 更多