如何使用java在Selenium WebDriver中处理iframe [英] How to handle iframe in Selenium WebDriver using java

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

问题描述

<div>    
  <iframe id="cq-cf-frame ">    
    <iframe id="gen367">   
      <body spellcheck="false" id="CQrte" style="height: 255px; font-size: 12px; font-family:tahoma,arial,helvetica,sans-serif; background-image: url(&quot;/libs/cq/ui/widgets/themes/default/ext/form/text-bg.gif&quot;); background-repeat: repeat-x; background-attachment: fixed;">
        <p>4t43t4<br></p>
      </body >
    </iframe>
  </iframe>    
</div> 

在这种情况下, iframe 下有的iframe 。我必须选择外部 iframe 转到内部 iframe 并写入内部的主体的iframe

In this scenario there is an iframe under iframe. And I have to select the outer iframe to go to inner iframe and write in the body which is in the inner iframe.

接下来,我必须从内部 iframe 出来到外部 iframe 然后点击OK按钮(位于外部 iframe )。

Next, I have to come out from the inner iframe to outer iframe and click on OK button, (which is in the outer iframe).

以下是我的代码

/*Line 1 */ driver.switchTo().frame("cq-cf-frame");
/*     2 */ driver.findElement(By.css("#extdd-9 > div.tblRow >  input.edititem").click();
/*     3 */ driver.switchT().Frame("cq-gen379");
/*     4 */ driver.findElement(By.id("CQrte").sendKeys("Tnx");  
/*     5 */ selenium.selectFrame("relative=up");       
/*     6 */ driver.findElement(By.xpath("//button[text()='OK']")).click(); 

以下是我的问题:

我的测试代码工作正常,直到第4行,即写入正文,但我想从内到外出来 iframe 它说元素 //按钮[text()='确定'] 未找到。

My test code is working fine up to line number 4 i.e. writing into the body, but I want to come out from inner to outer iframe it says that the element //button[text()='OK'] not found.

我尝试使用索引,父母,亲戚,但没有运气。

I tried with using index, parent, relative, but had no luck.

注意:如果我不选择内框(cq-gen379)。我'我可以点击确定按钮。

NOTE: If I don’t select inner frame (cq-gen379). I'm able to click on OK button.

推荐答案

在Webdriver中,你应该使用 driver.switchTo()。def aultContent(); 退出一个框架。
您需要先退出所有帧,然后再次切换到外框。

In Webdriver, you should use driver.switchTo().defaultContent(); to get out of a frame. You need to get out of all the frames first, then switch into outer frame again.

// between step 4 and step 5
// remove selenium.selectFrame("relative=up");
driver.switchTo().defaultContent(); // you are now outside both frames
driver.switchTo().frame("cq-cf-frame");
// now continue step 6
driver.findElement(By.xpath("//button[text()='OK']")).click(); 

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

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