org.openqa.selenium.NoSuchFrameException:使用selenium和Java在模式对话框中查找具有的元素时,无法定位框架

org.openqa.selenium.NoSuchFrameException:使用selenium和Java在模式对话框中查找具有的元素时,无法定位框架,java,selenium,xpath,css-selectors,webdriverwait,Java,Selenium,Xpath,Css Selectors,Webdriverwait,我想创建一个Selenium测试,其中使用了一个模态对话框。如果我想在div中使用该元素,我会得到一条错误消息,即找不到该元素。我还尝试切换到活动元素,或切换到帧。如果我这样做,我得到的错误,帧无法找到 这是html代码: 这是我切换到模态的想法: driver.switchTo().frame(driver.findElement(By.xpath("/html/body/div[8]/div")); 这就是我得到的错误: org.openqa.selenium.NoS

我想创建一个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测试实现这一点。

此错误消息

org.openqa.selenium.NoSuchFrameException: Unable to locate frame: 805833b6-6961-41e5-8bdf-3393a28e0ad9
…表示您标识为
的代码实际上不是

从HTML标记来看,该元素似乎位于中,其中有两个按钮。要单击
元素tobelickable()
所需的两个按钮,您可以使用以下任一选项:

  • 点击威特

    • css选择器

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-primary[ng-show^='firstStep.state.spaces.length']"))).click();
      
      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-primary' and text()='Weiter']"))).click();
      
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-default' and text()='Weiter']"))).click();
      
  • 点击Abbrechen

    • css选择器

      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.btn.btn-primary[ng-show^='firstStep.state.spaces.length']"))).click();
      
      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-primary' and text()='Weiter']"))).click();
      
      new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@class='btn btn-default' and text()='Weiter']"))).click();
      

参考文献 您可以在中找到有关的详细讨论:


您从中得到了什么:
列表按钮=driver.findElements(按.tagname(“按钮”);for(WebElement按钮:buttons){System.out.println(button.getText());}WebElement modal=driver.findElement(By.className(“模态内容”);驱动程序.切换到().框架(模态);List buttonsInModal=driver.findElements(按.tagname(“按钮”));对于(WebElement button:ButtonSnmodal){System.out.println(button.getText());}
?谢谢,这非常有效。你能告诉我如何使用下拉菜单和输入字段来实现这一点吗?@tobias听起来是一个完全不同的问题。你能根据你的新要求提出一个新问题吗?Stackoverflow贡献者将很乐意帮助您。好的,今天我将用新的源代码开始一个新问题这是我的新问题