尝试在 ID 字段中输入数据,但看到以下消息:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"attribute.ID"}
尝试了各种元素,但它无法在屏幕上找到输入字段。
在 FireFox 中的检查器滚动标题下使用检查元素 (Q)
iframe#iframe01 > html..js... > body... > div#cpo-folder > div.cpo-content-wrapper > div.cpo-content > iframe#iframe02 > html > frameset > frame > html... > body#SearchFilter > div.dt-filter.filter > form#filterForm > table.tbody > tr > input
HTML 代码
<html class="..." lang="en">
<iframe name="iframe01" id="iframe01" src="..." title="Navigation" scrolling="no" lang="en">
<html class="..." lang="en">
<iframe id="iframe02" name="iframe02" class="..." title="Main" frameborder="0" lang="en">
<html lang="en">
<frameset rows="150px,*" border="0">
<frame name="frame01" src="..." title="Results" noresize="">
<html ...>
<body id="SearchFilter" onunload="destroy()">
<h1> Identifier Search </h1>
<div class="dt-filter filter" style="display: block">
<form id="filterForm" action="..." method="POST" target="Results">
<table>
<tbody>
<tr>
<td class="label" nowrap="">ID</td>
<td><input name="attribute.ID" size="20" tabindex="1" type="text"></td>
<td class="label" nowrap=""></td>
<td><tabindex=4></tabindex=4></td>
</tr>
<tr>
</tbody>
</table>
</form>
</div>
</body>
</html>
</frame>
</frameset>
</html>
</iframe>
</html>
</iframe>
Java 代码:
public static void click_IdentifierSearch() throws InterruptedException{
driver.switchTo().defaultContent();
driver.switchTo().frame("iframe01");
driver.switchTo().frame("iframe02");
Thread.sleep(2000);
driver.findElement(By.id("attribute.ID")).sendKeys("123456789");
}
最佳答案
实际上 input
元素位于三个框架内,而您只切换两个框架,您还需要切换第三个框架,并且 input
元素具有 name 属性值 attribute .ID
而不是 id,所以你应该尝试如下:-
driver.switchTo().defaultContent();
driver.switchTo().frame("iframe01");
driver.switchTo().frame("iframe02");
driver.switchTo().frame("frame01");
driver.findElement(By.name("attribute.ID")).sendKeys("123456789");
注意 :- driver.switchTo().frame();
用于切换 frame
和 iframe
两者。
关于java - 无法定位元素消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38792135/