如何使用 java 在 Selenium WebDriver 中处理 iframe

编程入门 行业动态 更新时间:2024-10-10 21:27:35
本文介绍了如何使用 java 在 Selenium WebDriver 中处理 iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

<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("/libs/cq/ui/widgets/themes/default/ext/form/text-bg.gif"); background-repeat: repeat-x; background-attachment: fixed;"><p>4t43t4<br></p></正文 ></iframe></iframe>

在这种情况下,iframe 下有一个 iframe.我必须选择外部 iframe 转到内部 iframe 并写入内部 iframe 中的正文.

接下来,我必须从内层 iframe 出来到外层 iframe 并点击 OK 按钮,(在外层 iframe>).

以下是我的代码

/*Line 1 */driver.switchTo().frame("cq-cf-frame");/* 2 */driver.findElement(By.css("#extdd-9 > div.tblRow > input.edititem").click();/* 3 */driver.switchTo().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 它说元素 //button[text()='OK'] 未找到.

我尝试使用索引、父级、亲属,但没有成功.

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

解决方案

在 Webdriver 中,您应该使用 driver.switchTo().defaultContent(); 来跳出框架.您需要先退出所有框架,然后再切换到外框架.

//步骤 4 和步骤 5 之间//移除 selenium.selectFrame("relative=up");driver.switchTo().defaultContent();//你现在在两个框架之外driver.switchTo().frame("cq-cf-frame");//现在继续第 6 步driver.findElement(By.xpath("//button[text()='OK']")).click();

<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>

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.

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

Following is my code

/*Line 1 */ driver.switchTo().frame("cq-cf-frame"); /* 2 */ driver.findElement(By.css("#extdd-9 > div.tblRow > input.edititem").click(); /* 3 */ driver.switchTo().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();

Following is my problem:

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.

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

解决方案

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

本文发布于:2023-11-27 16:25:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638741.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   java   Selenium   WebDriver   iframe

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!