Click() -没有在foreach循环中执行

本文关键字:foreach 循环 执行 Click | 更新日期: 2023-09-27 18:17:53

我希望有人能帮忙。我正试图编写一个针对搜索功能的测试,当用户在搜索框中输入几个字符时,您将获得与您输入的标准匹配的建议。在下面的例子中,我在搜索框中输入单词"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. 

多谢

Click() -没有在foreach循环中执行

Click不执行,因为您从未通过条件。value是一个字符串。它不是网页元素。你应该比较web元素的文本与字符串:

if(value.Equals(suggestion.Text))

还要注意-您传递'ebration'作为列表项值。我没有看到任何与此文本相同的列表项。如果你想点击每一个包含ebration的项目,你需要检查suggestion.Text.Contains(value)。尽管您的场景说您应该只单击一个项目……这里需要foreach循环吗?