org.openqa.selenium.NoAlertPresentException:当前没有模态对话框打开

时间:2018-04-02 16:24:37

标签: java selenium

我正在编写一小段代码来自动完成日常任务。作为此任务的一部分,我登录到我们的内部网站并单击特定选项卡,该选项卡会提示一个对话框,我需要单击是或否来禁用。我使用了一个警告声明,但它仍然抛出以下错误。我也是在职业生涯中第一次这样做。从来没有这样做过。有人可以帮我这个吗?

线程中的异常" main" org.openqa.selenium.NoAlertPresentException:当前没有模态对话框打开

我的代码:

package test;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class automate {

    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.gecko.driver","C:\\Users\\405325\\eclipse-workspace\\Monitoring\\geckodriver.exe");
        WebDriver driver=new FirefoxDriver();
        driver.get("https://www.mywebsite.com");
        driver.findElement(By.name("userName")).sendKeys("myusername");
        driver.findElement(By.name("passWord")).sendKeys("mypassword");
        driver.findElement(By.name("loginForm")).click();
        driver.findElement(By.xpath("//*[@id=\"tabIndent\"]/div/table/tbody/tr[7]/td[2]/a/font")).click();

        Alert alert = driver.switchTo().alert();

        driver.findElement(By.xpath("//*[@id=\"dialog20HideableContent\"]/table/tbody/tr[1]/td/form/div/table/tbody/tr/td[2]/div/table[1]/tbody/tr/td")).click();

    }

}

1 个答案:

答案 0 :(得分:0)

根据您的问题,您看到的代码试验和错误似乎对话框模态对话框并调用click()你必须按如下方式诱导 WebDriverWait

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"dialog20HideableContent\"]/table/tbody/tr[1]/td/form/div/table/tbody/tr/td[2]/div/table[1]/tbody/tr/td"))).click();