Selenium和iframe

stu*_*ped 20 iframe selenium

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

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("on the tab that loads the iframe")然后selenium.getHtmlSource().从这个来源,我至少可以找到我感兴趣的链接.我做了一个,selenium.click("//span[text()='Link']")但似乎没有做任何事情.有什么想法吗?

这是代码:

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");  
Run Code Online (Sandbox Code Playgroud)

Dan*_*ard 40

我猜你正在使用Selenium 1.0.你看过Selenium 2.0和WebDriver吗?我找到了以下内容,它对我有用:

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

driver.switchTo().frame("foo");
WebElement editable = driver.switchTo().activeElement(); 
editable.sendKeys("Your text here"); 
Run Code Online (Sandbox Code Playgroud)

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

((JavascriptExecutor) driver).executeScript("document.body.innerHTML = '<br>'"); 
Run Code Online (Sandbox Code Playgroud)

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

一旦完成,请记住切换出框架(因为所有进一步的交互将与此特定框架一起):

driver.switchTo().defaultContent();
Run Code Online (Sandbox Code Playgroud)

我在http://code.google.com/p/selenium/wiki/FrequentlyAskedQuestions上找到了这个