无法在 Selenium Webdriver(java) 中找到 iframe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11866983/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 06:43:44  来源:igfitidea点击:

Unable to locate iframe in Selenium Webdriver(java)

javatestingiframeautomationselenium-webdriver

提问by chivas_hvn

I want to select a element of iframe which is located with in a popup window. I am able go inside popup window but not able to locate iframe. Below is html code of popup window.

我想选择一个位于弹出窗口中的 iframe 元素。我可以进入弹出窗口,但无法找到 iframe。下面是弹出窗口的 html 代码。

<html>
<head></head>
<body>
<iframe width="100%" height="100%" border="0" src="/some/url.do?parameter=getData">
<html>
 .
 .
 <table id="ec_table" class="tableRegion" width="20%" cellspacing="1" cellpadding="2" 
 border="0">
 <tr class="even">
 <td>
 <input type="radio" value="173" name="hier_data_id">
 </td>
 </tr>
 .
 .
 </html>
 </iframe>
 </body>
 </html>

Here I want to click Radio button which is located inside iframe. I used below code to switch with in iframe but it is not switching to iframe.

在这里,我想单击位于 iframe 内的单选按钮。我使用下面的代码在 iframe 中切换,但它没有切换到 iframe。

 driver.switchTo().frame(myD.findElement(By.tag("iframe")));

As iframe doesn't have id, i'm finding difficulty locate elements inside iframe.

由于 iframe 没有 id,我发现很难在 iframe 中定位元素。

Does anyone know how I could do it..?

有谁知道我该怎么做..?

Thanks in advance.

提前致谢。

采纳答案by StatusQuo

You can switch to a frame by its index . Try the following:

您可以通过索引切换到框架。请尝试以下操作:

//go to popup
//switch to the first frame , assuming there's only one frame with no id or name
driver.switchTo().frame(0);
driver.findElement(By.name("hier_data_id")).click();

回答by Rahul P

As iframe has id refer HEREand your answer is as follows

由于 iframe 有 id 参考这里,你的答案如下

<iframe width="100%" height="100%" border="0" id="iframe"
                  src="/some/url.do?parameter=getData">

and your code is

你的代码是

driver.switchTo().frame(myD.findElement(By.id("iframe")));

回答by DMart

//iframe[@src="/some/url.do?parameter=getData"]

//iframe[@src="/some/url.do?parameter=getData"]

But this might be a timing & loading issue. So you might be trying to write/read/interact with the iframe before the content has loaded. Try checking for the element before selecting it. And wait if it's not present.

但这可能是时间和加载问题。因此,您可能会在内容加载之前尝试写入/读取/与 iframe 交互。在选择元素之前尝试检查它。如果它不存在,请等待。

回答by ryan.sagar

here are the code for selecting the iFrame and enter text "Test data"

这是选择 iFrame 并输入文本“测试数据”的代码

$x = $I->grabAttributeFrom('//iframe', 'id');
$I->switchToIframe($x);
$I->fillField('#tinymce', 'Test data');
$I->switchToWindow();`

If you have two iFrame one after another You can switch through this function

如果你有两个 iFrame 一个接一个 可以通过这个功能切换

$I->switchToWindow();