Driver.getPageSource() в Selenium отличается от источника, просматриваемого в браузере.JAVA

Программисты JAVA общаются здесь
Anonymous
Driver.getPageSource() в Selenium отличается от источника, просматриваемого в браузере.

Сообщение Anonymous »

Я пытаюсь захватить исходный код из URL-адреса, указанного в HTML-файл, с помощью Selenium, но я не знаю почему, я не получаю точный исходный код, который мы видим в браузере.
Ниже приведен мой Java-код для захвата источника в HTML-файл:

Код: Выделить всё

private static void getHTMLSourceFromURL(String url, String fileName) {

WebDriver driver = new FirefoxDriver();
driver.get(url);

try {
Thread.sleep(5000); // The page gets loaded completely

List pageSource = new ArrayList(Arrays.asList(driver.getPageSource().split("\n")));

writeTextToFile(pageSource, originalFile);

} catch (InterruptedException e) {
e.printStackTrace();
}

System.out.println("quitting webdriver");
driver.quit();
}

/**
* Creates a file with 'fileName' and writes the content
*
* @param content
* @param fileName
*/
private static void writeTextToFile(List content, String fileName) {
PrintWriter pw = null;
String outputFolder = ".";
File output = null;
try {
File dir = new File(outputFolder + '/' + "HTML Sources");
if (!dir.exists()) {
boolean success = dir.mkdirs();
if (success == false) {
try {
throw new Exception(dir + " could not be created");
} catch (Exception e) {
e.printStackTrace();
}
}
}

output = new File(dir + "/" + fileName);
if (!output.exists()) {
try {
output.createNewFile();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
pw = new PrintWriter(new FileWriter(output, true));
for (String line : content) {
pw.print(line);
pw.print("\n");
}
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
pw.close();
}

}
Почему это происходит? Как WebDriver отображает страницу? А как браузер показывает исходник?

Вернуться в «JAVA»