[英]How to handle multiple pop-ups with selenium firefox webdriver
[英]Unable to handle Windows 10 pop-ups using Robot class
我正在嘗試在 Windows 10 的 Internet Explorer 中運行以下代碼。
---------------測試---------------公共類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 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(筆記本電腦)中運行它時,它不起作用。
有人可以幫忙嗎。 謝謝
您真正想做的是使用代理,而不是使用像 Java Robot 類這樣的 hack 進行基本身份驗證。 這是使用browserup 代理的解決方案。
首先,將 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>
然后在您的測試中使用 browserup 代理。 首先,您需要運行的導入如下:
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();
調整 autoAuthorization 行以使其適用於您的域和相關的基本身份驗證憑據應該是一項相對簡單的工作。
使用代理的優點是:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.