Java 无法使用Robot类处理Windows 10弹出窗口

Java 无法使用Robot类处理Windows 10弹出窗口,java,selenium-webdriver,awtrobot,Java,Selenium Webdriver,Awtrobot,我正在尝试在Internet Explorer Windows 10中运行以下代码 ----------------------------测试------------------公共类抽样测试{ public static void main(String args[]) throws AWTException, InterruptedException{ System.setProperty("webdriver.ie.driver", "path//IEDriverServer.e

我正在尝试在Internet Explorer Windows 10中运行以下代码

----------------------------测试------------------公共类抽样测试{

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();
}
}` --------------------助手方法-----------

` 公共类助手方法{

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

当我尝试在Windows7(桌面)中运行上述脚本时,它工作正常。但是,当我尝试在Windows10(笔记本电脑)中运行相同的程序时,它不起作用


有人能帮忙吗。感谢您不要使用类似黑客的Java Robot类进行基本身份验证,您真正想要做的是使用代理。这里有一个使用的解决方案

首先,将browserup代理依赖项添加到maven POM.xml中(这是假设您使用的是maven,但它在Java项目中是相当标准的)

然后,您应该能够复制/粘贴并尝试的示例测试是:

// 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();
调整自动授权行以使其适用于您的域和相关的基本身份验证凭据应该是一项相对简单的工作

使用代理的优点是:

  • 这是跨浏览器兼容的
  • 这是跨操作系统兼容的
  • 这将适用于本地和远程WebDriver实例(但是,对于远程实例,运行浏览器的计算机将需要访问运行代理的计算机,并且您需要为代理传递有效的IP地址,而不是本地主机)
  • 与各种机器人类黑客相比,尝试点击不同操作系统级别的对话框所需的代码要少得多

在Win 10中运行时会发生什么情况?是否有任何错误消息?这正是为什么将解决方案与机器人类一起破解通常是一个坏主意,它不是真正的跨平台。你想解决的真正问题是什么?是否需要输入基本身份验证凭据才能访问要测试的网站?如果是这样,还有更好的方法。@Ardesco-是的,我正在尝试验证这里弹出的Windows身份验证。请您提出任何替代方案。@MateMrše-我没有收到任何错误,但是Robot类没有将凭据发送到弹出窗口。它继续加载,最后测试失败,出现超时异常。这最适用于基本身份验证,但当用于NTLM
AuthType.NTLM
时,它抛出-
java.lang.UnsupportedOperationException:HTTP授权不支持AuthType NTLM
。是否有其他处理NTLM的方法?请提供帮助。您需要了解NTLM正在向服务器传输什么(是带有某种身份验证密钥的头?)。我建议您使用Charles这样的代理查看网络流量,以查看在执行NTLM身份验证时发送的内容。另一种选择是这样的:
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;
// 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();