首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用机器人类处理Windows 10弹出窗口

无法使用机器人类处理Windows 10弹出窗口
EN

Stack Overflow用户
提问于 2019-04-29 11:18:46
回答 1查看 1.9K关注 0票数 2

我试图在,Windows 10中运行下面的代码。

类SampleTest {

代码语言:javascript
复制
public static void main(String args[]) throws AWTException, InterruptedException{
    System.setProperty("webdriver.ie.driver", "path//IEDriverServer.exe");
    WebDriver driver = new InternetExplorerDriver();
    driver.get("url");
    HelperMethods.validateSplash();
}

}}

公共类HelperMethods {

代码语言:javascript
复制
public static void validateSplash() throws AWTException, InterruptedException{
    HelperMethods.ctrlV("username");
    HelperMethods.pressTab();
    Thread.sleep(2000);
    HelperMethods.ctrlV("password");
    HelperMethods.pressEnter();
}

private static void ctrlV(String stringToPaste) throws AWTException{
    Robot robot = new Robot();
    StringSelection strToPaste = new StringSelection(stringToPaste);
    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(strToPaste, null);            
    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_V);
    robot.keyRelease(KeyEvent.VK_CONTROL);
}

private static void pressTab() throws AWTException{
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_TAB);
    robot.keyRelease(KeyEvent.VK_TAB);
}

private static void pressEnter() throws AWTException{
    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_ENTER);
    robot.keyRelease(KeyEvent.VK_ENTER);
}

}`

当我尝试在Windows 7(桌面)中运行上面的脚本时,它运行得很好。但是当我尝试在Windows 10(笔记本电脑)上运行相同的程序时,它就不能工作了。

有人能帮忙吗。谢谢

EN

回答 1

Stack Overflow用户

发布于 2019-04-30 13:53:59

您真正想要做的是使用代理,而不是为Basic Auth使用像Java机器人类这样的黑客。这里有一个使用浏览器代理的解决方案。

首先,将browserup代理依赖项添加到您的maven POM.xml中(假设您使用的是maven,这对于POM.xml项目来说是非常标准的)。

代码语言:javascript
复制
<dependency>
    <groupId>com.browserup</groupId>
    <artifactId>browserup-proxy-core</artifactId>
    <version>1.0.0</version>
    <scope>test</scope>
</dependency>

然后在测试中使用browserup代理。首先,您需要运行的导入如下:

代码语言:javascript
复制
import com.browserup.bup.BrowserUpProxy;
import com.browserup.bup.BrowserUpProxyServer;
import com.browserup.bup.client.ClientUtil;
import com.browserup.bup.proxy.auth.AuthType;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;

然后,您应该能够复制/粘贴和试用的一个示例测试是:

代码语言:javascript
复制
// Start up the browserup proxy server
BrowserUpProxy browserUpProxyServer = new BrowserUpProxyServer();
//Specify domain that uses basic auth, then the username and password followed by auth type
browserUpProxyServer.autoAuthorization("the-internet.herokuapp.com", "admin", "admin", AuthType.BASIC);
browserUpProxyServer.start();
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(browserUpProxyServer);

// Configure IEDriver to use the browserup proxy
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.setProxy(seleniumProxy);
WebDriver driver = new InternetExplorerDriver(ieOptions);

//Go to a site with basic auth enabled and check it all works
driver.get("https://the-internet.herokuapp.com/basic_auth");

//Clean up after test has finished
driver.quit();
browserUpProxyServer.stop();

调整autoAuthorization行以使其适用于您的域和相关的基本auth凭据应该是一项相对简单的工作。

使用代理的优点是:

  • 这是兼容跨浏览器的。
  • 这是兼容交叉操作系统的。
  • 这将适用于本地和远程WebDriver实例(但是对于远程实例,运行浏览器的机器将需要访问正在运行代理的机器,并且需要为代理传递有效的IP地址,而不是本地主机)
  • 与各种机器人类黑客相比,尝试并点击不同操作系统级别对话框的代码要少得多。
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55902247

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档