【问题标题】:Using Selenium + Java robot + Scrapbook to download a complete web page使用 Selenium + Java 机器人 + Scrapbook 下载一个完整的网页
【发布时间】:2016-02-08 19:23:21
【问题描述】:

我正在尝试通过 Selenium、java 机器人类和 ScrapBook firefox 扩展下载一个完整的网页(包括图像、css、scipts 等)。 我首先尝试使用机器人的 CTRL+S 方法,但没有成功,因为它无法按 ENTER。所以我决定在 Firefox 上安装 ScrapBook(有一种方法不显示下载对话框),并用普通的机器人类进行了尝试,效果很好。但我需要将它与硒一起使用以进行快速测试。 所以这里是源代码:

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

}

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

【问题讨论】:

    标签: java selenium firefox awtrobot


    【解决方案1】:

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

    【讨论】:

    • 我发布答案是因为我认为它可能在有人无法使用浏览器下载对话框保存页面的情况下有用。
    猜你喜欢
    • 2017-05-12
    • 2020-05-12
    • 1970-01-01
    • 2013-05-12
    • 1970-01-01
    • 2011-05-20
    • 2017-06-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多