Selenium和iframe [英] Selenium and iframe

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

问题描述

当我点击页面上的标签页时,我有一个加载的iframe。当我使用Firebug来查看IE8上的iframe时,我看到的是:

I have an iframe that gets loaded when i click on a tab on a page. When i use Firebug to look at the iframe on IE8, all i see is:

iframe id = tabContextFrame class = contextFrame contentEditable = inherit src = /xyz.dt?forward=show&layouttype=NoHeader&runid=1234 name = tabContextFrame url = / xyz.dt?forward = show& layouttype = NoHeader& runid = 1234 scrolling = auto

就是这样。无法看到iframe下面的层次结构。我想点击iframe中的链接。为了找到iframe中的元素,我做了一个 selenium.click(在加载iframe的选项卡上)然后 selenium.getHtmlSource( )。从这个来源,我至少可以找到我感兴趣的链接。我做了一个 selenium.click(// span [text()='Link'])但它似乎没有做任何事情。有什么想法吗?

and that's it.The hierarchy below the iframe can't be seen. I want to click on a link within the iframe. To find the elements within the iframe, I did a selenium.click("on the tab that loads the iframe") and then selenium.getHtmlSource(). From this source, I can at least locate my link of interest. I did a selenium.click("//span[text()='Link']") but it doesn't seem to do anything. Any ideas please?

以下是代码:

selenium.click("//span[text()='tab that loads iframe']");   
Thread.sleep(5000);   
selenium.selectFrame("tabContextFrame");         
selenium.mouseOver("//span[text()='Link']");   
selenium.mouseDown("//span[text()='Link']");  
selenium.mouseUp("//span[text()='Link']");   
Thread.sleep(5000);   
selenium.selectFrame("null");  


推荐答案

我猜你正在使用Selenium 1.0。你看过Selenium 2.0和WebDriver吗?我发现以下内容对我有用:

I'm guessing you are using Selenium 1.0. Have you looked at Selenium 2.0 and WebDriver. I found the following and it worked for me:


问:如何输入contentEditable iframe?答:假设
iframe被命名为foo:

Q: How do I type into a contentEditable iframe? A: Assuming that the iframe is named "foo":

driver.switchTo().frame("foo");
WebElement editable = driver.switchTo().activeElement(); 
editable.sendKeys("Your text here"); 

有时这不起作用,这是因为iframe
没有任何内容。在Firefox上,您可以在sendKeys之前执行以下

Sometimes this doesn't work, and this is because the iframe doesn't have any content. On Firefox you can execute the following before "sendKeys":

((JavascriptExecutor) driver).executeScript("document.body.innerHTML = '<br>'"); 

这是必需的,因为iframe默认没有内容:
没有什么可以发送键盘输入到。这个方法调用插入一个
空标记,它可以很好地设置所有内容。

This is needed because the iframe has no content by default: there's nothing to send keyboard input to. This method call inserts an empty tag, which sets everything up nicely.

一旦完成,请记得切换出框架(因为所有进一步的$) b $ b交互将使用此特定框架):

Remember to switch out of the frame once you're done (as all further interactions will be with this specific frame):

driver.switchTo().defaultContent();


我在http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions

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

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