org.openqa.selenium.NoSuchFrameException:使用 Selenium 和 Java 在模态对话框中查找元素时无法定位框架

org.openqa.selenium.NoSuchFrameException: Unable to locate frame while finding an element with in a modal dialog box using Selenium and Java

我想创建一个使用模态对话框的 Selenium 测试。如果我想使用 div 中的元素,我会收到无法找到该元素的错误消息。我也试过切换到活动元素,或者切换到框架。如果我这样做,我会得到错误,找不到框架。

这是 html 代码:

这是我切换到模式的想法:

driver.switchTo().frame(driver.findElement(By.xpath("/html/body/div[8]/div"));

这是我得到的错误:

org.openqa.selenium.NoSuchFrameException: Unable to locate frame: 805833b6-6961-41e5-8bdf-3393a28e0ad9

最后我想按下模态框内的按钮。我希望有人可以帮助我通过 selenium java 测试来实现这一目标。

这个错误信息...

org.openqa.selenium.NoSuchFrameException: Unable to locate frame: 805833b6-6961-41e5-8bdf-3393a28e0ad9

...意味着您识别为 <iframe> 实际上不是 <iframe>

从 HTML 标记看来该元素在 Modal Dialog Box and there are two buttons in it. To click on the two buttons you need to induce for the elementToBeClickable() and you can use either of the following :

  • 点击Weiter

    • cssSelector:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-primary[ng-show^='firstStep.state.spaces.length']"))).click();
      
    • xpath:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-primary' and text()='Weiter']"))).click();
      
  • 点击Abbrechen

    • cssSelector:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-default[data-dismiss='modal']"))).click();
      
    • xpath:

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-default' and text()='Weiter']"))).click();
      

参考

您可以在以下位置找到关于 的详细讨论: