使用Selenium处理InnerFrames [英] Handling InnerFrames with Selenium

查看:113
本文介绍了使用Selenium处理InnerFrames的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须测试使用内部框架的老式Web应用程序。就像:

I have to test an "old fashioned" web application which uses inner frames.Just like:

<html>
....
<frameset name="fooSet">
<frame name="myFrame">
<html>
.....
<frame name="whatever">
.....
</html>
....
</html>

我用rekursions读取所有帧...我现在唯一的问题是如果有存储框架处理程序的方法就像windowsHandler..like:

I used rekursions to read all frames... the only question I have now is if there is a way to store a handler for the frame just like a windowsHandler..like:

WebDriver driver = new HtmlUnitDriver();
driver.get(Constant.URL);
WebDriver fooDriver = driver.switchTo().frame("myFrame");
String handler = fooDriver.getWindowHandle();

我的意图是,一旦焦点丢失,我可以轻松导航到框架,否则我必须再次遍历所有帧以获得我想要选择的那个。

My intention is that I could easily navigate to the frame once the focus is lost, otherwise I have to iterate again over all frames to get the one I want to pick.

推荐答案

不,没有。

事实上,使用Selenium处理 iframe 元素非常棘手。

In fact, handling iframe elements with Selenium is very tricky.

切换到一个框架有三个选项:

You have three options for switching into a frame:


  1. 使用其索引(0,1,...,N-1)

  2. 使用其名称或ID(如上例所示)

  3. 使用 WebElement 您从DOM获得的对象

  1. Using its index (0, 1, ..., N-1)
  2. Using its name or ID (as you did in the example above)
  3. Using a WebElement object that you obtain from the DOM

但您只有一个选项可以转出:

But you have only one option for switching out:


  1. driver.switchTo()。defaultContent()

  1. driver.switchTo().defaultContent()

一旦你换出一帧,你实际上已经完全了。

And once you've switched out of a frame, you in fact switched out "all the way up".

所以如果你想的话你用简单的DFS递归帧,很可能你错了。

So if you thought you were recursing the frames with a simple DFS, it is most likely you were wrong.

我发现这样做的方法是留下面包屑。换句话说,在切换到帧之前,我记住从DOM根到该帧的整个路径。

The way I found to do it is by "leaving bread crumbs". In other words, before switching into a frame, I "memorize" the entire path from the root of the DOM to that frame.

切换后,因为我是回到根,我执行到帧的整个路径(当然切换到该帧除外),然后继续到它旁边的帧。

After switching out, because I'm "back in the root", I perform the entire path to the frame (except for switching into that frame of course), and then continue to the frame next to it.

请注意,一旦您切换到一个框架,您存储的所有以前的 WebElement 对象都变得陈旧,因此您必须在切换后再次找到它们。

Be aware that once you've switched into a frame, all previous WebElement objects you were storing have become "stale", so you would have to find them again after switching out.

PS:我发现几乎所有网站都在 iframe 标签内存储广告,所以我怀疑它是旧的时尚东西。

P.S.: I found that almost all websites store advertisements inside iframe tags, so I doubt it's an "old fashion" thing.

无论如何,这是我使用的代码(在Java中):

Anyway, here is the code that I used (in Java):

private void searchFrames(List<Integer> route)
{
    doWhateverYouWannaDoHere();
    if (route.size() < MAX_DEPTH)
    {
        int numOfFrames = webDriver.findElements(By.tagName("iframe")).size();
        for (int i=0; i<numOfFrames; i++)
        {
            try
            {
                webDriver.switchTo().frame(i);
                List<Integer> newRoute = new ArrayList<Integer>(route);
                newRoute.add(i);
                searchFrames(newRoute);
                webDriver.switchTo().defaultContent();
                for (int j : route)
                    webDriver.switchTo().frame(j);
            }
            catch (NoSuchFrameException error)
            {
                break;
            }
            catch (Exception error)
            {
            }
        }
    }
}

要启动此过程,请调用 searchFrames(new ArrayList< Integer>())

In order to start the procedure, call searchFrames(new ArrayList<Integer>()).

希望有帮助......

Hope it helps...

更新:

此方法不仅在每个帧内执行所需的操作,而且还在DOM本身中执行所需的操作。如果你想避免使用DOM本身,那么在 try 子句中的第一行后,将调用移到方法'doWhateverYouWannaDoHere',即之后 webDriver.switchTo()。frame(i)

This method performs the desired action not only inside each frame, but also in the DOM itself. If you want to avoid the DOM itself, then move the call to method 'doWhateverYouWannaDoHere' after the first line in the try clause, i.e., after webDriver.switchTo().frame(i).

这篇关于使用Selenium处理InnerFrames的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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