使用Selenium + Java机器人+剪贴簿下载完整的网页 [英] Using Selenium + Java robot + Scrapbook to download a complete web page

查看:204
本文介绍了使用Selenium + Java机器人+剪贴簿下载完整的网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Selenium,java机器人类和ScrapBook firefox扩展下载一个完整的网页(包括图像,CSS,scipt等)。
我第一次尝试CTRL + S方法与机器人,但不工作,因为它不能按ENTER键。所以我决定在Firefox上安装ScrapBook(有一种不显示下载对话框的方法),并用简单的机器人类来试用,效果很好。但是我需要用硒来快速测试。
所以这里是源代码:

  package selenium.inexample; 

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

public class SelRobot {

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

//我使用我的firefox配置文件剪贴簿
ProfilesIni配置文件=新ProfilesIni();
FirefoxProfile myprofile = profile.getProfile(default);

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

//仅当等待页面加载
时,才添加这个参数driver.getPageSource();

//机器人使用键盘与浏览器交互并使用剪贴簿
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ALT);
robot.keyRelease(KeyEvent.VK_ALT); (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);
}

}

问题是剪贴簿会下载页面以local \temp\result作为临时文件夹,然后在会话过期时删除所有子文件夹和文件。我需要将它作为firefox配置文件文件夹中的permament页面下载。
机器人在页面加载后直接使用剪贴簿。
我在代码中使用我的Firefox配置文件,因为它安装了剪贴簿扩展。
我想指出,普通的机器人(剪贴簿)工作正常,但问题是当我使用Selenium(有一些配置我需要做?)。
另外,请注意代码只是一个简单的例子。如果有效,我会在生产中使用类似的东西。

解决方案

我找到了解决方案!
问题是剪贴簿总是将页面下载到配置文件夹中。当我使用Selenium时,总是会创建一个临时的配置文件,即使我创建了一个新的配置文件或者使用了一个已经存在的配置文件。
所以,我只是配置剪贴簿下载页面到任何其他文件夹不同的配置文件。
只需按下Alt + k,然后点击工具>选项>组织。在那里,我只是改变了下载的文件夹。这样,硒不会删除页面,因为它不是它的临时配置文件数据的一部分。


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.

这篇关于使用Selenium + Java机器人+剪贴簿下载完整的网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆