简体   繁体   中英

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 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM