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

时间:2018-06-18 07:44:23

标签: 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,因为当前帧不是红色窗口而是默认窗口

3 个答案:

答案 0 :(得分:1)

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

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

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

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

正如评论中所提到的,如果你需要在验证它是正确的iframe后从列表中访问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
  }
}

答案 1 :(得分:0)

使用driver.switchTo().defaultContent();

或使用:driver.switchTo().parentFrame();

答案 2 :(得分:0)

通过使用@Shivam Mishra的想法,我创建了此方法:

public static void switchToTheParentFrame(final WebDriver driver) {
        driver.switchTo().defaultContent();
        iframeList.remove(iframeList.size() - 1);
        final Iterator<By> itr = iframeList.iterator();
        if (!iframeList.isEmpty()) {
            while (itr.hasNext()) {
                final WebElement frame = driver.findElement(itr.next());
                try {
                    driver.switchTo().frame(frame);
                } catch (final WebDriverException wde) {
                    LOG.error("Error while switching to the frame : " + frame.toString(), wde);
                }
            }
        }
    }

iFrameList是一个全局变量,每次测试时我都会使用一个空列表将其初始化。

然后,我可以继续测试而不会崩溃。因此,我强迫Selenium返回良好的父级框架。我希望这会对某人有所帮助!