Java selenium getPageSource 不工作
Java selenium getPageSource not working
我需要程序中给出的url的来源。但是程序 returns 只有一些 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();
}
}
问题是您获取页面源代码太早 - 当时页面尚未加载。使用 Explicit Wait 等待页面上的特定元素变为可见。
例如,等待照片列表块可见:
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("photoListBlock"));
以上问题可以通过Implicit和Explicit等来解决。
在这里,我尝试使用隐式等待您的代码。请试试这个。它使用以下代码对我有用。
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个字符
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();
希望对您有所帮助
我需要程序中给出的url的来源。但是程序 returns 只有一些 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();
}
}
问题是您获取页面源代码太早 - 当时页面尚未加载。使用 Explicit Wait 等待页面上的特定元素变为可见。
例如,等待照片列表块可见:
WebDriverWait wait = new WebDriverWait(webDriver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("photoListBlock"));
以上问题可以通过Implicit和Explicit等来解决。 在这里,我尝试使用隐式等待您的代码。请试试这个。它使用以下代码对我有用。
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个字符
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();
希望对您有所帮助