简体   繁体   中英

How to accept java applet warning alert in selenium?

I have a web page to test ,in which an java applet also runs.When i logon to the page it takes 30 to 50 seconds to load the java applet and it is showing java unsigned applet security alert.So when i run my selenium scripts,the execution is getting stopped whenever this alert popped.I cannot able to accept the java alert because i don't know when it is going to pop.

So my problem here is , I was forced to wait until my applet loads applet alert comes, accept it and then execute my steps.Is there a way to write a listener kind of a thing so that whenever the java security alert comes it should automatically accept the alert and continue with the execution of my script.

If you know where and when... it should be easy to anticipate the alert message's appearance and call acceptAlert (or whichever is the correct method name, not sure).

Otherwise, you should be able to write a conditional for isAlertPresent (again I'm not sure of the exact method name since I'm more familiar with phpUnit than java selenium). And if present you can take action to dismiss said alert if you wish.

I had solved this months ago in PHP, when I thought the alert might be there, to get its text. If getText returns characters, then there is indeed an alert present and you should be able to accept it or dismiss it.

If i understand your question correctly, you want to poll the presence of the alert on screen.

To correctly wait for Alert, you got WebDriverWait implemented especially for it:

WebDriverWait wait = new WebDriverWait(driver, 5); //5 seconds wait
element.click() //trigger for alert to appear
wait.until(ExpectedConditions.alertIsPresent());

Now you can handle the alert:

Alert alert = driver.switchTo().alert();
alert.accept(); //Accepting the alert

Of course, instead of accepting you can do something else, according to this .

Also, you can implement some simple alert validation like this:

public Boolean isAlertExistAndAccepted(WebDriver driver) {
        try {
            Alert alert = driver.switchTo().alert();
            alert.accept();
            return true;
        } catch (NoAlertPresentException e) {
            return false;
        }
    }

In do-while mechanism something like this one:

do {

} while ( !isAlertExist() );

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