【问题标题】:How to accept alert in selenium which is coming up in a different window?如何接受在不同窗口中出现的硒警报?
【发布时间】:2018-09-20 20:23:18
【问题描述】:

场景:有 2 个窗口打开。当我单击第二个窗口上的按钮时,会打开第三个窗口,并且焦点会自动转移到第三个窗口。将在第三个窗口接受警报。

问题:我无法接受警报,因为它来自不同的窗口。

发现:我认为这是 Selenium 的局限性。如果警报出现在单击按钮的同一窗口中,则我们拥有 DOM,因此我们能够与警报进行交互。但是在这种情况下,警报位于不同的窗口中,因此浏览器的状态是锁定的。

尝试过的解决方案:使用 javascript、selenium action class 等尝试了所有可能的方法,但它不起作用。

一些尝试的方法如下

//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();*/

【问题讨论】:

  • 您需要切换到第三个窗口才能点击警报。你可以使用硒switch to window method
  • 您无法在浏览器上执行任何操作,因为警报仍处于打开状态。浏览器处于锁定状态,控件未退出点击事件。
  • 您没有对浏览器本身执行任何操作。使用 switch 方法,您要求 Selenium 使用要单击警报的新窗口。先试试看,如果它不适合你,请告诉我。
  • 这些是基本的东西..我显然已经尝试过了。
  • 你在任何地方都没有提到这些“显而易见”的基本东西,所以这就是我让你尝试的原因。无论如何,您可以尝试新贡献者的答案,并祝您找到解决方案。

标签: javascript java selenium selenium-webdriver alert


【解决方案1】:

为了消除某些疑虑,Alert 是通过 JavaScript 生成的,并且绝不是 HTML DOM 的一部分。

接受解除一个警报,您必须始终诱导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();

【讨论】:

  • 这不是等待警报接受。当我通过 element.click() 方法单击按钮时,这里发生了什么,它不会从该方法中出来,以便执行下一条语句,我可以在其中编写代码来处理警报。
【解决方案2】:

以下解决方案运行良好,可用于类似场景。

我们必须使用 java.awt 包的 Robot 类。 在下面的代码中 Alt+space+c 将关闭所有打开的窗口。在这里关闭警报。

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");
    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-16
    相关资源
    最近更新 更多