【问题标题】:org.openqa.selenium.NoSuchFrameException: Unable to locate frame while finding an element with in a modal dialog box using Selenium and Javaorg.openqa.selenium.NoSuchFrameException:使用 Selenium 和 Java 在模式对话框中查找元素时无法定位框架
【发布时间】:2021-04-01 00:30:53
【问题描述】:

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

这是html代码:

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

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

这是我得到的错误:

org.openqa.selenium.NoSuchFrameException:无法定位框架: 805833b6-6961-41e5-8bdf-3393a28e0ad9

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

【问题讨论】:

  • 你从:List<WebElement> buttons = driver.findElements(By.tagname("button")); for (WebElement button: buttons) { System.out.println(button.getText()); } WebElement modal = driver.findElement(By.className("modal-content")); driver.switchTo().frame(modal); List<WebElement> buttonsInModal = driver.findElements(By.tagname("button")); for (WebElement button: buttonsInModal) { System.out.println(button.getText()); }得到什么?

标签: java selenium xpath css-selectors webdriverwait


【解决方案1】:

此错误消息...

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

...暗示您识别为<iframe>WebElement 实际上不是<iframe>

从 HTML 标记看来,该元素位于 Modal Dialog Box 内,其中有两个按钮。要点击这两个按钮,您需要将WebDriverWait 诱导为elementToBeClickable(),您可以使用以下任一Locator Strategies

  • 点击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();
      
  • 点击缩写

    • 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();
      

参考

你可以在NoSuchElementException找到详细讨论:

【讨论】:

  • 感谢这完美。你能告诉我如何使用下拉菜单和输入字段来制作它吗?
  • @tobias 听起来完全不同。你能根据你的新要求提出一个新问题吗? Stackoverflow 贡献者将很乐意为您提供帮助。
  • 好的,我今天将用新的源代码打开一个新问题
猜你喜欢
  • 1970-01-01
  • 2018-08-01
  • 2020-12-26
  • 2020-11-06
  • 2022-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-05
相关资源
最近更新 更多