【问题标题】:How would you check if a popup window exists using selenium webdriver?您将如何使用 selenium webdriver 检查是否存在弹出窗口?
【发布时间】:2014-07-23 13:23:16
【问题描述】:

我正在一个应用程序上运行链接测试,其中一个链接会弹出一个登录弹出窗口。有没有办法检查?我尝试将其视为警报,但没有奏效。

try
   {
     WebDriverWait wait = new WebDriverWait(driver, 2);
     wait.until(ExpectedConditions.alertIsPresent());
     Alert alert = driver.switchTo().alert();
     alert.accept();
     reportAlertPresent = true;
    }

【问题讨论】:

    标签: java selenium-webdriver popupwindow


    【解决方案1】:

    driver.switchTo().alert() 上,可以抛出org.openqa.selenium.NoAlertPresentException,这是未选中的(即RuntimeException)。

    您可以捕获它并可能在循环中执行此操作。

    【讨论】:

      【解决方案2】:

      如果您的弹出窗口位于 iframe 内,则

       WebElement frameID = driver.findElement(By.(locator));
       driver.switchTo().frame(frameID);
      

      如果是窗口,可以使用窗口句柄。以下链接可能会有所帮助

      http://www.thoughtworks.com/products/docs/twist/2.3/help/how_do_i_handle_popup_in_selenium2.html

      How to handle Pop-up in Selenium WebDriver using Java

      【讨论】: