无法找到元素消息

时间:2016-08-05 14:50:17

标签: java selenium

尝试在ID字段中输入数据,但看到以下消息:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"attribute.ID"}

尝试了各种元素,但它无法在屏幕上找到输入字段。

在Inspector滚动标题

下的FireFox中使用Inspect Element(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"); 
}

1 个答案:

答案 0 :(得分:1)

实际上input元素在三个帧内,而你只需要切换两个帧,你需要切换第三帧,而input元素的名称属性值为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();用于切换frameiframe两者。