简体   繁体   English

无法接受Selenium Java中的警报-Firefox浏览器

[英]Not able to accept the alert in selenium java - Firefox browser

enter image description here I will open a window to perform some actions and open a new tab in same window. 在此处输入图像描述,我将打开一个窗口以执行一些操作,并在同一窗口中打开一个新标签。 Once new tab is opened, I switch to previous window to continue the execution and once its done, i will switch to the second window to perform actions. 打开新选项卡后,我切换到上一个窗口以继续执行,完成后,我将切换到第二个窗口以执行操作。 When the URL is opened in 2nd window, it is not accepting the alert and throws an exception. 在第二个窗口中打开URL时,它不接受警报并引发异常。

org.openqa.selenium.UnhandledAlertException: : org.openqa.selenium.UnhandledAlertException::

This works fine when I open a new Firefox driver in a new window. 当我在新窗口中打开新的Firefox驱动程序时,此方法工作正常。

    try
    {
        Alert alert=DRIVER.switchTo().alert();
        alert.accept();
    }
    catch(NoAlertPresentException e)
    {
        System.out.println("No alert");
    }

I tried with the below code as well - 我也尝试了以下代码-

    WebDriverWait wait= new WebDriverWait(driver, 10);
    Alert alert = wait.until(ExpectedConditions.alertIsPresent());
    alert.accept();

Code to switch to window - 切换到窗口的代码-

    String windowHandle = DRIVER.getWindowHandle();                  
    DRIVER.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"t");
    ArrayList<String> tabs = new ArrayList<String>(DRIVER.getWindowHandles());
    DRIVER.switchTo().window(tabs.get(1)); 
    DRIVER.switchTo().window((String) DRIVER.getWindowHandles().toArray()[0]);
    DRIVER.switchTo().window((String) DRIVER.getWindowHandles().toArray()[1]);

Could someone please tell what the issue is? 有人可以告诉我问题是什么吗? Thanks in advance! 提前致谢!

with Thread.sleep(500); Thread.sleep(500); before DRIVER.switchTo().window(tabs.get(1)); DRIVER.switchTo().window(tabs.get(1)); should work 应该管用

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

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