简体   繁体   中英

How to accept alert in selenium which is coming up in a different window?

Scenario: There are 2 windows open. When I am clicking a button on the 2nd window, a 3rd window is opening and the focus is automatically shifting to 3rd window. An alert is coming on the 3rd window to accept.

Issue: I am not able to accept the alert as it's coming in a different window.

Findings: I think it's the limitation of Selenium. If the alert is coming on the same window where the button is clicked, we have the DOM, so we are able to interact with the alert. But in this case the alert is in a different window, so the state of the browser is locked.

Tried solution: Tried all the possible ways by using javascript, selenium action class etc, but it's not working.

some of the tried methods are as below

//e.click();
                        /*Actions ac = new Actions(driver);

                        ac.sendKeys(Keys.ENTER).build().perform();*/
                        String onClickScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('click', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject){ arguments[0].fireEvent('onclick');}";
                        JavascriptExecutor jse = (JavascriptExecutor)driver;
                        jse.executeScript(onClickScript, e);

                    /*  Actions asd = new Actions(driver);
                        asd.clickAndHold(e).perform();
                        Thread.sleep(1000);
                        asd.release().perform();*/

To clear certain doubts an Alert is generated through JavaScript and is never a part of the HTML DOM .

To accept or dismiss an Alert you must always induce WebDriverWait for the alert to be present as follows:

import org.openqa.selenium.Alert;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
//other code
Alert myAlert = new WebDriverWait(driver, 10).until(ExpectedConditions.alertIsPresent());
//accept an alert
myAlert.accept();
//dismiss an alert
myAlert.dismiss();

The below solution is working fine and can be used in similar scenario.

We have to use the Robot class of java.awt package. In the below code Alt+space+c will close any opened window. Here closing the alert.

public void closeAlert(String strControlName, String delayTime) {
    Robot rb;
    int timeInSec = Integer.parseInt(delayTime);
    try {
        rb = new Robot();
        rb.keyPress(KeyEvent.VK_ENTER); //for clicking on the button or link
        rb.keyRelease(KeyEvent.VK_ENTER);
        Log.info("Wait for "+timeInSec+" Secs");
        Thread.sleep(timeInSec*1000);
        rb.keyPress(KeyEvent.VK_ALT); 
        rb.keyPress(KeyEvent.VK_SPACE);
        rb.keyPress(KeyEvent.VK_C);
        rb.keyRelease(KeyEvent.VK_C);
        rb.keyRelease(KeyEvent.VK_SPACE);
        rb.keyRelease(KeyEvent.VK_ALT); 
        Log.info("Successfully clicked on '"+strControlName+ "' and closed the Alert");
    } catch (Exception e) {
        Log.info("Failed click on '"+strControlName+ "' and close the Alert");
    }


}

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