【问题标题】:Not able to accept the alert in selenium java - Firefox browser无法接受 selenium java - Firefox 浏览器中的警报
【发布时间】:2018-10-10 08:59:10
【问题描述】:

enter image description here我将打开一个窗口来执行一些操作并在同一窗口中打开一个新选项卡。打开新选项卡后,我切换到上一个窗口以继续执行,一旦完成,我将切换到第二个窗口执行操作。在第二个窗口中打开 URL 时,它不接受警报并引发异常。

org.openqa.selenium.UnhandledAlertException: :

当我在新窗口中打开新的 Firefox 驱动程序时,这可以正常工作。

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

我也试过下面的代码 -

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

切换到窗口的代码 -

    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]);

有人能告诉我问题是什么吗?提前致谢!

【问题讨论】:

  • 我是 Java Selenium 的新手。任何建议都会有很大帮助!
  • 如果警报属于操作系统(文件下载对话框、代理身份验证等),selenium 永远不会到达它(甚至看到)。在这种情况下,您需要避免警报。
  • 的代码试验更新问题...切换到第二个窗口...接受警报...
  • 当我在新窗口中打开此 URL 时,它能够处理警报。
  • 我们没有办法做到这一点吗?

标签: java selenium-webdriver


【解决方案1】:

DRIVER.switchTo().window(tabs.get(1)); 之前使用Thread.sleep(500); 应该可以工作

【讨论】:

  • 我查看了在选项卡之间切换的代码。我还有一个 Thread.sleep(100);打开新标签后。
  • 能否提供网址让我自己试试?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-07
  • 2019-06-02
  • 2017-08-16
  • 2021-02-19
  • 1970-01-01
  • 1970-01-01
  • 2018-10-04
相关资源
最近更新 更多