简体   繁体   English

如何接受不同窗口中出现的硒警报?

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

Scenario: There are 2 windows open. 场景:有2个窗口打开。 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. 如果警报位于单击按钮的同一窗口中,则我们具有DOM,因此我们能够与警报进行交互。 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. 尝试过的解决方案:通过使用javascript,硒行动类等尝试了所有可能的方法,但是它不起作用。

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 . 为了消除某些疑问, 警报是通过JavaScript生成的,并且绝不是HTML DOM的一部分。

To accept or dismiss an Alert you must always induce WebDriverWait for the alert to be present as follows: 接受关闭 警报,您必须始终诱使WebDriverWait使警报出现 ,如下所示:

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. 我们必须使用java.awt包的Robot类。 In the below code Alt+space+c will close any opened window. 在下面的代码中,Alt + space + c将关闭所有打开的窗口。 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");
    }


}

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

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