gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:52:21 25 4
gpt4 key购买 nike

<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 中的正文。

接下来,我要从内部 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(); 来跳出框架。您需要先退出所有框架,然后再切换到外框架。

// 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 - 如何使用 java 在 Selenium WebDriver 中处理 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9942928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com