Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java selenium getPageSource不工作_Java_Selenium_Selenium Firefoxdriver - Fatal编程技术网

Java selenium getPageSource不工作

Java selenium getPageSource不工作,java,selenium,selenium-firefoxdriver,Java,Selenium,Selenium Firefoxdriver,我需要在程序中给出的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 loadPag

我需要在程序中给出的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();        

}
}

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

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

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

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

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

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

上述问题可以通过隐式和显式等待来处理。 在这里,我尝试了隐式等待和您的代码。请试试这个。下面的代码对我有效

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

上述问题可以通过隐式和显式等待来处理。 在这里,我尝试了隐式等待和您的代码。请试试这个。下面的代码对我有效

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

我只是在@alecxe上添加更多信息回答alecxe提供的解决方案工作得非常好

eclipse的控制台输出大小默认仅为80000个字符

窗口>首选项,转到运行/调试>控制台部分>然后禁用限制控制台选项

或者将数据写入文件

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

希望这对您有所帮助

我只是在@alecxe上添加更多信息回答alecxe提供的解决方案工作得非常好

eclipse的控制台输出大小默认仅为80000个字符

窗口>首选项,转到运行/调试>控制台部分>然后禁用限制控制台选项

或者将数据写入文件

    File file = new File("path/filename.txt");
    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    BufferedWriter bw = new BufferedWriter(fw);
    bw.write(content);
    bw.close();
希望这对你有帮助