java - 关闭另一个 iframe 时,Selenium 切换到错误的 iframe

标签 java selenium iframe

我正在使用 Selenium 来执行一些测试。我的问题是我有 4 个嵌套的 iframe,当我关闭一个 iframe 时,Selenium 会切换到错误的 iframe。

为了帮助您理解我的问题,我有一个小草图: enter image description here

所以我的第一个框架(黑色框架)打开一个嵌套框架(灰色框架),嵌套框架打开另一个框架(橙色框架),然后再次打开另一个框架(白色框架)。问题是:当我关闭白色框架时,Selenium 会切换到灰色框架,但通常应该切换到橙色框架。

当框架关闭时,我使用driver.switchTo().parentFrame();。每次都工作得很好,但在这种情况下,它不起作用,我找不到问题。

我尝试了一些测试代码

if (driver.findElements(By.xpath("//iframe")).size() > 0) {
     driver.switchTo().frame(driver.findElements(By.xpath("//iframe")).size() - 1);
}

但它也不起作用。

如果有人有想法或遇到同样的问题,我很乐意尝试您的答案。

编辑:这是我的堆栈跟踪,因此在执行某些操作之前,我切换到所需的帧:

frameLocator = By.xpath: //*[@id='greyWindow']
___Before switch and before execute___ 
CurrentFrame =: title=defaultWindow

___After switch and before execute___ 
CurrentFrame = <IFRAME src=grey id=greyWindow>

frameLocator = By.xpath: //*[@class='redWindow']
___Before switch and before execute___ 
CurrentFrame = <IFRAME src=grey id=greyWindow>

___After switch and before execute___
CurrentFrame = <IFRAME src=red class=redWindow>

frameLocator = By.xpath: //iframe[contains(@src,'whiteWindow')]
___Before switch and before execute___ 
CurrentFrame = <IFRAME src=red class=redWindow>

___After switch and before execute___ 
CurrentFrame = <IFRAME src=whiteWindow>

Frame currently available : [[RemoteWebDriver: chrome on LINUX (5c408ef997226a10c864cbeec0a2472d)] 
-> tag name: iframe] | frame.getAttribute("innerHTML") = // Nothing appear
Frame currently available : [[RemoteWebDriver: chrome on LINUX (5c408ef997226a10c864cbeec0a2472d)]
-> tag name: iframe] | frame.getAttribute("innerHTML") = // Nothing appear

//Here i just put some value and close the frame as every frame before

___Before switch and after execute___ 
CurrentFrame = title=defaultWindow

___After switch and after execute___ 
CurrentFrame = title=defaultWindow

然后我得到了 nullPointerException 因为当前帧不是红色窗口而是默认窗口

最佳答案

为什么不通过名称或其他选择器切换到框架?

driver.switchTo().frame("name_or_id");

或者您可以尝试将框架保存在 WebElement 变量中,然后切换到该变量。例如:

WebElement iframeElement = driver.findElement(By.id("name"));
driver.switchTo().frame(iframeElement);

正如评论中提到的,如果您需要在验证列表正确后访问列表中的 iframe,您可以尝试:

List <WebElement> all_iframes = driver.findElements(By.tagName("iframe"));
for(WebElement iframe : all_iframes)
{
  if(iframe.getAttribute("name") == "your required name")
  { 
    //do some stuff
  }
}

关于java - 关闭另一个 iframe 时,Selenium 切换到错误的 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50904772/

相关文章:

javascript - jQuery跨域iframe高度问题

html - 如何使用 google doc iframe 禁用 pdf 查看器中的弹出选项?

javascript - 从 iframe 将 javascript 函数注入(inject)父窗口

java - 与 Java 相比,Objective-C 中类级别方法的区别和好处是什么

java - 如何在 JSoup 中使用 CSS 选择器来选择包含图像的所有元素,包括 data-src(HTML5 数据集)属性中的元素?

java - 检查 linux 命令是否输出空字符串

Java-为什么 Selenium Chrome Webdriver 使用我的真实 IP 地址而不是代理

java - 如何修复使用 DiffUtil 向 ListUpdateCallback 发送更新时得到的错误位置

java - 在 Java 中使用 Selenium 单击动态下拉 div

javascript - Selenium:执行 javascript 命令时测试卡住 "return document.readyState"