在selenium webdriver中等待声明和启动警报时类型不匹配:无法从警报转换为警报错误即将发生

时间:2017-07-18 06:59:16

标签: selenium selenium-webdriver

在下面的代码中虽然我已经在selenium webdriver中声明并初始化了一个警告,但是在selenium webdriver中出现错误。

Alert action = wait.until(ExpectedConditions.alertIsPresent());

在上面的警告中

  

类型不匹配:无法从警报转换为警报

即将正确宣布上述警报。

1 个答案:

答案 0 :(得分:0)

以下是您的问题的答案:

根据您的问题,您提供了以下代码:

Alert action = wait.until(ExpectedConditions.alertIsPresent());

从语法上讲,你是非常正确的。

观察代码块的以下快照,对alertIsPresent()的调用返回ExpectedCondition<Alert>,我们必须将其分配给Alert类的对象,您通过代码。

enter image description here

当您看到 Type mismatch: cannot convert from Alert to Alert

时,现在进入错误部分

最可能的原因可能是您的代码不完整或导入丢失。要使这行代码工作,您必须具备以下条件:

  1. 在使用实例之前启动WebDriverWait的实例。
  2. 制作合适的imports
  3. 一个例子:

    import org.openqa.selenium.Alert;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    //your code
    WebDriverWait wait8 = new WebDriverWait(driver, 10);
    Alert action = wait8.until(ExpectedConditions.alertIsPresent());
    
  4. 通过这个你应该看到错误。

    如果这回答你的问题,请告诉我。