Selenium - Save website including all images, css, dom

梦想的初衷 提交于 2019-12-23 04:28:13

问题


I would like to visit a page with selenium using firefox or chrome. When the page is loaded I would like to download all images, css, dom from the page.

I would like to store every image like I find them in:

chrome -> Tools->Development Tools -> Resources -> Images 

Is it possible to get access via selenium and save everything?

So far I only found this page, which has no interesting hints for me: http://ldanswers.org/wordpress/zisser/2014/11/24/save-whole-web-page-with-all-resources-in-selenium-webdriver/


回答1:


I found a solution to the problem by myself. The Problem is when you click on Save page as -> STRG + S an OS-Window pops up, which can't be managed by Selenium. I read about many possible solutions about that using tools like AutoIT, xnee, jna or Java Robot.

I didn't wanted to use such tools. So I searched for a firefox addon which is able to download a whole page (including images, css, html). After some seconds I found Scrapbook.

At last I downloaded the Addon File scrapbook-1.5.11-fx.xpi, modified some values in it and used it with selenium. It is working very vell.

Values I changed at /defaults/preferences/scrapbook-prefs.js which is

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

This config tells scrapbook to save the webpages at the defined path and that the shortcut for saving a page is STRG + SHIFT + D.

Now you only have to add the Plugin to the firefox profile,call a website and Send the shortcut to the browser.

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

I hope this helps someone!

Edit: If you want scrapbook to also save JavaScript per Default you have to change the file saver.js in the Plugin and set the value "script" : false to true.



来源:https://stackoverflow.com/questions/28114612/selenium-save-website-including-all-images-css-dom

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!