【问题标题】:WebDriver is not able to find out WebElement where as same css works fine when I use Selenium IDE当我使用 Selenium IDE 时,WebDriver 无法找到 WebElement,因为相同的 css 可以正常工作
【发布时间】:2015-02-28 11:52:27
【问题描述】:

我正在使用 Firefox WebDriver,在我的测试中,由于某种原因它无法找到 Link/Image WebElement。

当我使用 Selenium IDE 和相同的 CSS(不能与 WebDriver 一起使用)时,它工作得非常好。

谁能建议我在使用 WebDriver 时对 CSS 选择器的 xpath 进行哪些更改?我不知道我在这里错过了什么。

我的 HTML 页面代码如下所示:我想点击“新建”链接/图片。

<div id="toolbarcontainer">
<ul id="toolbarlist">
    <li>
        <a id="btnNewDashboard" title="New" onclick="javascript:$('#dialogAddTab').dialog('open'); return false;" href="#">
            <span class="toolbarlistimg"></span>
            <span class="toolbarlisttext"></span>
        </a>
    </li>
    <li></li>
    <li></li>
</ul> </div>

使用 Selenium IDE 录制和播放可以正常使用以下代码:

<tr> <td>click</td> <td>css=span.toolbarlisttext</td> <td></td> </tr>

但是当我在测试中使用下面的代码时,它不起作用...我尝试了以下 - 请建议

driver.findElement(By.cssSelector("span.toolbarlisttext")).click();
driver.findElement(By.className("toolbarlisttext")).click();
driver.findElement(By.xpath("//a/span[@class='toolbarlisttext']")).click();
driver.findElement(By.xpath("css=span.toolbarlisttext")).click();

谢谢,

更新:2015 年 1 月 2 日

新按钮/链接实际上在 iFrame 下,我完全忽略了 iFrame。之后,我可以点击新链接。答案如下。

【问题讨论】:

    标签: java html css selenium xpath


    【解决方案1】:

    我认为你需要在这里定位a标签:

    driver.findElement(By.id("btnNewDashboard")).click();
    

    或者,使用By.cssSelector

    driver.findElement(By.cssSelector("a#btnNewDashboard")).click();
    

    【讨论】:

    • 我也试过了..但在上面的帖子中没有提到..这也没有用:(。谢谢!
    • @PareshkumarChaudhari 好的,有什么症状?你有什么错误吗?
    • 我总是遇到同样的错误... WebElement 使用 so and so 方法,并且元素名称不存在或找不到。
    • @PareshkumarChaudhari 可能是这里发生了其他事情。例如,元素尚不存在,或页面未完全加载。你能显示你拥有的当前代码吗?谢谢。
    • 我实际上找到了解决方案。新按钮实际上在 iFrame 下。 :)
    【解决方案2】:

    我的一个朋友意识到“新建”链接/img 在 iF​​rame 中,因为它在 iFrame 中,所以我们必须先将焦点切换到 iFrame,然后单击元素。

    示例代码:

    // Switch the focus to iFrame first
    driver.switchTo().frame(driver.findElement(By.xpath("//*[@id='Framework_Client_BusinessIntelligence_0']/iframe")));
    // Click on Link/Button/Image present in iFrame
    driver.findElement(By.xpath("//*[@id='btnNewDashboard']")).click();
    

    谢谢,

    【讨论】:

      【解决方案3】:

      你试过了吗?

      driver.findElement(By.cssSelector("#toolbarlist > #btnNewDashboard > .toolbarlisttext")).click();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多