【问题标题】:Selenium Fluent WebDriver - Click does not work as expectedSelenium Fluent WebDriver - 单击无法按预期工作
【发布时间】:2015-06-29 08:48:38
【问题描述】:

我有一个分页列表,基本上是这样构造的(我使用的是 AngularJS):

<span id="latestResultsIndicator">    
    <ul>
        <li ng-click="Item.action()"><span>1</span></li>
        <li ng-click="Item.action()"><span>2</span></li>
    </ul>
</span>

这在 HTML 中效果很好,但是当我尝试使用 Selenium WebDriver 和 Fluent API 编写集成测试时遇到了问题。

具体来说,我想点击第二个li,为此我使用以下代码

I.Assert.Exists("#latestResultsIndicator");
var secondPageElement = I.Find("#latestResultsIndicator ul li:nth-child(2)");
I.Click(secondPageElement);

这实际上不起作用!如果我在 Chrome 控制台中使用 jQuery 执行 $("#latestResultsIndicator ul li:nth-child(2)").trigger("click") 则选择第二页,所以我知道选择器是正确的。

为了进一步测试,我添加了如下双击:

I.DoubleClick(secondPageElement);

我在这里注意到的是第一个 li 被选中。就好像它试图单击或选择错误的一样! (见图)。

发生了什么?

【问题讨论】:

    标签: angularjs selenium selenium-webdriver integration-testing


    【解决方案1】:

    我知道 Webdriver 在使用 css 伪选择器(例如 nth-child)时存在一些问题。您也许可以让它与 Xpath 一起工作,它既快速又灵活。

    var webDriver = (IWebDriver)I.Provider;
    webDriver.FindElement(By.XPath("//*[@id='latestResultsIndicator']//ul//li[2]")).Click();
    

    不确定它是否有效,因为我没有使用 FluentAutomation。

    【讨论】:

    • 谢谢,这是一个好主意,确实让我进步,但它只会引发另一个错误。我认为 Selenium 不会让我单击 li 元素。虽然如果我使用 Firefox IDE 插件它允许它!这是一个例外:unknown error: Element is not clickable at point (829, 177). Other element would receive the click: &lt;div paging="" page="pageOptions.Page" page-size="pageOptions.ResultsPerPage" total="pageOptions.TotalResults" paging-action="navigateToPage(page)" show-prev-next="false" adjacent="1" class="paginationContainer ng-isolate-scope"&gt;...&lt;/div&gt;
    • 看起来你的
    • 元素被什么东西覆盖了。我想你可以用 javascript 强制它,但我不知道如何用 FluentAutomation 做到这一点。
    【解决方案2】:

    您的问题中所写的 CSS 选择器不正确

    "#latestResultsIndicator ul li:nth-child(2)"
    

    UL 有你要找的 id,所以应该是

    "ul#latestResultsIndicator li:nth-child(2)"
    

    不确定这是否能解决您的问题,但这是一个开始的地方。如果没有,请更新结果

    【讨论】:

    • 谢谢史蒂夫,实际上这是我的问题的一个问题。我一定是在放的时候编辑错了。相应更新
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签