Using Selenium + Java robot + Scrapbook to download a complete web page

余生长醉 提交于 2019-12-13 00:56:56

问题


I'm trying to download a complete web page (including images, css, scipts, etc.) via Selenium, the java robot class and the ScrapBook firefox extension. I first tried the CTRL+S method with robot but doesn't worked because that could not press ENTER. So I decided to install ScrapBook on firefox (there's a method which do not displays the download dialog) and tried it with plain robot class, that worked fine. But I need to use it with selenium for rapid testing. So here is the source code:

package selenium.inexample;

import java.awt.*;
import java.awt.event.KeyEvent;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class SelRobot {

public static void main(String[] args) throws AWTException {

// I use my firefox profile for using scrapbook
    ProfilesIni profile = new ProfilesIni();
    FirefoxProfile myprofile = profile.getProfile("default");

    WebDriver driver = new FirefoxDriver(myprofile);
    driver.manage().window().maximize();
    driver.get("https://www.google.com.");

// This is added only for waiting for the page to load
    driver.getPageSource();

//Robot uses the keyboard for interacting with browser and using scrapbook
    Robot robot = new Robot();  
    robot.keyPress(KeyEvent.VK_ALT);
    robot.keyRelease(KeyEvent.VK_ALT);

    for(int i=0; i<5; i++) {
        robot.keyPress(KeyEvent.VK_RIGHT);
        robot.keyRelease(KeyEvent.VK_RIGHT);
    }
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
}

}

The problem is that scrapbook downloads the page to local\temp\result as a temporal folder, then it deletes all subfolders and files when the session expires. I need it to be downloaded as a permament page inside the firefox profile folder. The robot uses scrapbook directly once the page loads. I'm using my firefox profile in the code because it has the scrapbook extension installed. I want to point out that the plain robot (with scrapbook) works fine, but the problem is when I use Selenium (is there some config I need to do?). Also, please note the code is just a simple example. If it works, I'll use something similar in production.


回答1:


I found the solution!!! The problem was that Scrapbook always downloaded the page to the profile folder. When I used Selenium, a temporary Profile was always created, even If I created a new one or used an already existing one. So, I just configured scrapbook to download the page to any other folder diferent to the profile. Just pressed Alt+k, then Tools > Options > Organize. There I just changed the folder of the downloads. That way, selenium doesn't deletes the page because it's not part of it's temp profile data.



来源:https://stackoverflow.com/questions/35277621/using-selenium-java-robot-scrapbook-to-download-a-complete-web-page

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