简体   繁体   English

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

[英]Unable to handle Windows 10 pop-ups using Robot class

I am trying to run the below code in Internet Explorer, Windows 10.我正在尝试在 Windows 10 的 Internet Explorer 中运行以下代码。

----------------------------Test---------------public class SampleTest { ---------------测试---------------公共类SampleTest {

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----------- }` --------------------HelperMethods-----------

` public class HelperMethods { ` 公共类 HelperMethods {

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

}` }`

When I try to run the above script in Windows 7(Desktop), it is working fine.当我尝试在 Windows 7(桌面)中运行上述脚本时,它工作正常。 But when I try to run the same in Windows 10(laptop), it is not working.但是当我尝试在 Windows 10(笔记本电脑)中运行它时,它不起作用。

Can someone please help.有人可以帮忙吗。 Thanks谢谢

Instead of using a hack like Java Robot classes for Basic Auth, what you really want to do is use a proxy.您真正想做的是使用代理,而不是使用像 Java Robot 类这样的 hack 进行基本身份验证。 Here's a solution using the browserup proxy .这是使用browserup 代理的解决方案。

First of all add the browserup proxy dependency to your maven POM.xml (this is assuming you are using maven, it's pretty standard with Java projects though).首先,将 browserup 代理依赖项添加到您的 maven POM.xml(这是假设您使用的是 maven,不过它对于 Java 项目来说是非常标准的)。

<dependency>
    <groupId>com.browserup</groupId>
    <artifactId>browserup-proxy-core</artifactId>
    <version>1.0.0</version>
    <scope>test</scope>
</dependency>

Then use the browserup proxy in your tests.然后在您的测试中使用 browserup 代理。 First of all the imports you are going to need to run this are as follows:首先,您需要运行的导入如下:

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;

Then an example test that you should be able to copy/paste and try out is:然后,您应该能够复制/粘贴并尝试的示例测试是:

// 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();

It should be a relatively simple job to tweak the autoAuthorization line to make it work for your domain and your associated basic auth credentials.调整 autoAuthorization 行以使其适用于您的域和相关的基本身份验证凭据应该是一项相对简单的工作。

The advantages to using a proxy are:使用代理的优点是:

  • This is cross browser compliant这是跨浏览器兼容的
  • This is cross OS compliant这是跨操作系统兼容的
  • This will work with local and remote WebDriver instances (However with remote ones the machine that the browser is running on will need access to the machine that the proxy is running on and you need to pass in a valid IP address for the proxy, not localhost)这将适用于本地和远程 WebDriver 实例(但是对于远程实例,运行浏览器的机器将需要访问运行代理的机器,您需要为代理传递有效的 IP 地址,而不是本地主机)
  • It's a lot less code than various Robot class hacks to try and and click on varying different OS level dialogue boxes.与各种机器人类黑客相比,尝试和点击不同的操作系统级别的对话框所需的代码要少得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Selenium Firefox WebDriver处理多个弹出窗口 - How to handle multiple pop-ups with selenium firefox webdriver 使用Robot()处理弹出窗口的问题 - Issue with using Robot() to handle windows pop up 在Java弹出窗口中重复使用JTextField - Re-using JTextField in Java pop-ups 如何使用Selenium 2.0(WebDriver)处理Windows弹出窗口? - How to handle windows pop ups using selenium 2.0 (webdriver)? 在eclipse插件中动态更改弹出窗口 - Changing pop-ups dynamically in eclipse plugin 通过Selenium Webdriver(3.x)处理自定义弹出窗口(不是默认窗口) - Handling custom pop-ups (not the default windows one) through selenium webdriver (3.x) 有什么方法可以更改弹出窗口的默认阻止设置并重定向以允许使用 selenium java 在 chrome 中使用? - Is there any way to change default block setting of pop-ups and redirect to allow in chrome using selenium java? 是否可以使用 Java 中的 JOptionPane 同时显示多个弹出窗口? - Is it possible to have multiple pop-ups present at the same time using the JOptionPane in java? 处理在Java / Selenium中停止处理的浏览器弹出窗口 - Handling browser pop-ups that stop processing in Java/Selenium 我们是否应该为页面中出现的错误弹出窗口编写单独的页面对象? - Should we write separate page-object for the error pop-ups coming in a page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM