单击() - 未在我的foreach循环中执行

时间:2015-10-15 09:27:56

标签: c# selenium selenium-webdriver

我希望有人可以提供帮助。我正在尝试针对搜索功能编写测试,当用户在搜索框中输入几个字符时,会向您提供与您输入的条件匹配的建议。在下面的例子中,我输入了“' cel'进入搜索字段,我得到3个列表项。代码没有错误,foreach循环返回列表中的所有三个项,但是' suggestion.Click()'没有被执行,我不知道为什么。为了给出一些上下文,这是一个在线问候网站。

    <li ng-click="goSearch('Autocomplete', 'keyword')" ng-bind-html="searchForMsg()" class="ng-binding">Search for <strong>"cel"</strong></li>
    <li ng-repeat="suggestion in (suggestions = (allFacetsAutoComplete.facets | typeahead:moonpigSearchBox.term))" ng-bind-html="highlight(suggestion.DisplayName)" ng-click="selectSuggestion(suggestion)" ng-bind="suggestion.DisplayName" class="ng-binding ng-scope"><strong>cel</strong>ebration</li>
    <li ng-repeat="suggestion in (suggestions = (allFacetsAutoComplete.facets | typeahead:moonpigSearchBox.term))" ng-bind-html="highlight(suggestion.DisplayName)" ng-click="selectSuggestion(suggestion)" ng-bind="suggestion.DisplayName" class="ng-binding ng-scope">jewish <strong>cel</strong>ebrations</li>
    [Then(@"I select the following list item '(.*)' from my search")]
    public static void PreSelectedListOptions(string value)
    {
        var suggestedList = Driver.Instance.FindElements(By.CssSelector(".list-reset li"));
        foreach(IWebElement suggestion in suggestedList)
        {
            if(value.Equals(suggestion))
            {
                suggestion.Click();
            }
        }
    }
And I perform a partial search for 'cel'
And I select the following list item 'ebration' from my search
//Note: I have just copied a part of the scenario. 

非常感谢

1 个答案:

答案 0 :(得分:3)

未执行Click,因为您从未传递条件。 value是一个字符串。这不是网络元素。您应该将web元素的文本与您拥有的字符串进行比较:

if(value.Equals(suggestion.Text))

另请注意 - 您将'ebration'作为列表项值传递。我没有看到任何包含此确切文本的列表项。如果您要点击包含ebration的每个项目,则需要检查suggestion.Text.Contains(value)。虽然你的场景说你应该只点击一个项目..你在这里需要foreach循环吗?