Selenium - 保存网站,包括所有图像,CSS,dom

Joh*_*hv1 0 java selenium dom save

我想使用firefox或chrome访问包含selenium的页面.当页面加载时,我想从页面下载所有图像,css,dom.我想存储每个图像,比如我在chrome中找到它们 - >工具 - >开发工具 - >资源 - >图像.是否可以通过硒获取并保存所有内容?

到目前为止,我只找到了这个页面,对我没有任何有趣的提示:http: //ldanswers.org/wordpress/zisser/2014/11/24/save-whole-web-page-with-all-resources-in-硒的webdriver /

Joh*_*hv1 5

我自己找到了解决问题的方法.问题是,当您单击保存页面 - > STRG + S时,会弹出一个操作系统窗口,这是Selenium无法管理的.我使用AutoIT,xnee,jna或Java Robot等工具阅读了许多可能的解决方案.

我不想使用这样的工具.所以我搜索了一个能够下载整个页面的firefox插件(包括图像,css,html).几秒钟后我找到了剪贴簿.

最后我下载了Addon File剪贴簿-1.5.11-fx.xpi,修改了一些值并将其与selenium一起使用.它工作得非常好.

价值观在我改变/defaults/preferences/scrapbook-prefs.js这是

pref("scrapbook.data.default", false);
pref("scrapbook.data.path", "/Path/to/store/webpage");
pref("scrapbook.key.save", "D");
Run Code Online (Sandbox Code Playgroud)

这个配置告诉剪贴簿保存网页的定义的路径和用于保存页面的快捷方式是STRG + SHIFT + d.

现在,您只需将插件添加到firefox配置文件,调用网站并将快捷方式发送到浏览器.

FirefoxProfile oProfile = new FirefoxProfile();
        File extension = new File("scrapbook-1.5.11-fx.xpi");
        try {
            oProfile.addExtension(extension);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        WebDriver driver = new FirefoxDriver(oProfile);
        driver.get("https://google.com");
        new Actions(driver).sendKeys(Keys.chord(Keys.CONTROL,Keys.SHIFT, "D")).perform();
Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助别人!

编辑:如果您希望剪贴簿还为每个默认值保存JavaScript,则必须更改插件中的文件saver.js并将值"script":false设置为true.