Kelper February 2016

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

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

Answers


Kelper February 2016

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.

Post Status

Asked in February 2016
Viewed 2,250 times
Voted 5
Answered 1 times

Search




Leave an answer