简体   繁体   中英

Selenium 2 WebDriver UnhandledAlertException Java

Now before I start getting scolded, I DID read through most of the existing questions about this and applied different solutions (which mostly repeat the same thing), but it still does not work for me. I have a maven project with all necessary dependencies, and the website in testing is done specifically for IE and requires me to have a specific certificate in order to access it. I have the certificate for it, and when I go onto the website, before it loads the page it asks me to confirm that I have the certificate and I need to confirm on the pop-up window, THEN the login page fully loads. 图像显示我接受证书之前的页面

我手动接受证书后加载的页面

NetBeans持久错误消息

I have done to typical:

WebDriverWait wait = new WebDriverWait(driver, 3);
try {
    // Handle alert box
    driver.navigate().to("https://ke.m-pesa.com/ke/");
    wait.until(ExpectedConditions.alertIsPresent());
    Alert alert = driver.switchTo().alert();
    alert.accept();
} 
catch(Exception e) {
    //whatever
}

Can you tell me where I am going wrong? So far I have used only Selenium RC up till now so this webdriver stuff is still kind of new to me. Please tell me if you need any more info I need to provide. Why do I still get the UnhandledAlertException?? and why can't I access the login page until I manually press the OK button?

Did you try using Robot? Something like :

     Alert alert = driver.switchTo().alert();
     Robot a = new Robot();
     a.keyPress(KeyEvent.VK_ENTER);

Why robot and not Actions

From this answer :

There is a huge difference in terms of how do these tools work. Selenium uses the WebDriver API and sends commands to a browser to perform actions (through the "JSON wire protocol").

Java AWT Robot uses native system events to control the mouse and keyboard.

If you are doing browser automation, ideally, you don't ever use things like Robot since usually the functionality provided by selenium is more than enough. Though, there are cases when there is a browser or native OS popup opened, for example, to upload/download a file - this is something that can be also solved with Robot -

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM