加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Selenium – driver.getPageSource()与从浏览器中查看的

发布时间:2020-12-14 05:59:48 所属栏目:Java 来源:网络整理
导读:我试图使用selenium从指定的 HTML文件中捕获源代码,但我不知道为什么,我没有得到我们从浏览器中看到的确切源代码. 下面是我在Java文件中捕获源代码的java代码 private static void getHTMLSourceFromURL(String url,String fileName) { WebDriver driver = n
我试图使用selenium从指定的 HTML文件中捕获源代码,但我不知道为什么,我没有得到我们从浏览器中看到的确切源代码.

下面是我在Java文件中捕获源代码的java代码

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<String> pageSource = new ArrayList<String>(Arrays.asList(driver.getPageSource().split("n")));

        writeTextToFile(pageSource,originalFile);

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

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

/**
 * creates file with fileName and writes the content
 * 
 * @param content
 * @param fileName
 */
private static void writeTextToFile(List<String> 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如何呈现页面?浏览器如何显示源代码?

解决方法

有几个地方你可以从中获取来源.你可以试试
String pageSource=driver.findElement(By.tagName("body")).getText();

看看会出现什么.

通常,您不需要等待页面加载.Selenium会自动执行此操作,除非您有单独的Javascript / Ajax部分.

您可能想要添加您所看到的差异,以便我们了解您的真正含义.

Webdriver不会自己呈现页面,它只是在浏览器看到它时呈现它.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读