关闭另一个时,Selenium切换到错误的iframe [英] Selenium switch to the wrong iframe when closing another one

查看:490
本文介绍了关闭另一个时,Selenium切换到错误的iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Selenium to perform some test. My problem is that i've 4 nested iframes and when i close one, Selenium switch to the wrong one.

为了帮助你理解我的问题,我有一个小草图:

To help you to understand my problem i have a little sketch :

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

So my first frame (the black one) open a nested frame (the grey one) who open another one (the orange one) and again, open another one (the white one). The problem is : when i close the white frame, Selenium switch to the grey one but is should normaly switch to the orange one.

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

When a frame close, i use driver.switchTo().parentFrame();. It worked fine every time but in that case, it doesn't work and i can't find the issue.

我已经尝试了一些测试代码为

I've tried some test code as

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

但它也不起作用。

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

If anyone have an idea or encounter the same problem, i'll be glad to try your answer.

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

Edit : Here is my stacktrace, so before performing some action, i switch to the wanted frame :

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

Then I got nullPointerException because the current frame isn't the red window but the default window

推荐答案

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

By using the idea of @Shivam Mishra, i've created this method :

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 是一个全局变量,我在每次测试时使用空列表初始化。

iFrameList is a global variable that i init at every test with an empty list.

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

Then i can continue my test without crashing. So i force Selenium to come back to the good parent frame. I hope this will help somebody !

这篇关于关闭另一个时,Selenium切换到错误的iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆