使用Webdriver,C#选择具有动态ID的下拉元素

时间:2015-06-17 11:50:14

标签: c# xpath dynamic selenium-webdriver css-selectors

我有一个我试图处理的情况。我正在使用webdriver,C#。当我尝试使用CSSSelector时,它只是将参数作为字符串读取。请仔细研究一下。这是HTML。

<div class="select2-container select2" id="s2id_UserRole" style="width: 100%;">
<a href="javascript:void(0)" class="select2-choice" tabindex="-1">   
***<span class="select2-chosen" id="select2-chosen-7">&nbsp;</span>***
<abbr class="select2-search-choice-close"></abbr>   
<span class="select2-arrow" role="presentation">
<b role="presentation"></b></span></a><label for="s2id_autogen7" class="select2-offscreen"></label>
<input class="select2-focusser select2-offscreen" type="text" aria-haspopup="true" role="button" aria-labelledby="select2-chosen-7" id="s2id_autogen7">
<div class="select2-drop select2-display-none select2-with-searchbox">   
<div class="select2-search">       
<label for="s2id_autogen7_search" class="select2-offscreen"></label>       
<input type="text" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false" class="select2-input" role="combobox" aria-expanded="true" aria-autocomplete="list" aria-owns="select2-results-7" id="s2id_autogen7_search" placeholder="">   
</div>   
<ul class="select2-results" role="listbox" id="select2-results-7">   </ul></div>
</div>

我尝试获取ID的元素是使用此类ID的下拉列表:id="select2-chosen-7"。 &#34;选择 - &#34;是静态的,数字部分是动态的。阅读论坛后,所有建议似乎都没有答案。最后,这是我正在使用的代码仍然无效 -

IWebElement DropDownPath = driver.FindElement(By.CssSelector("*[id^='select2-chosen-'][id*='select2-chosen-']"));
SelectElement DropDown = new SelectElement(DropDownPath);
DropDown.SelectByText(UserConstants.UserRoleText);

2 个答案:

答案 0 :(得分:2)

你应该使用递归:

public void ConutNumber(int count)
{
    ...
    GoThroughElements(count);
    ...
}

public void GoThroughElements(int count, List<String> recurseValues = new List<String>())
{
    foreach(String value in ValuesAdd1)
    {
        recurseValues.Add(value);
        if(count == 1)
        {
            // In deepest recursion iterate through the line of values
            foreach(String i in recurseValues)
                Console.WriteLine(i);
        }
        else if(count > 1)
        {
            GoThroughElements(--count, recurseValues);
        }
        else
        {
            throw new Exception("Wrong count!");
        }
    }
}

不要忘记查看无效值的计数。谨慎使用递归,如果错误的情况被忽视,它可能很容易导致内存问题。

答案 1 :(得分:2)

如果不要求将ValuesAddX分开,那么你可以拥有一个数组数组,并预测:

public static string[][] ValuesAdd = 
    {
        new [] { "a", "b", "c" },
        new [] { "1", "2", "3" },
        new [] { "x", "y", "z" },
    };

public void NestedForeach() 
{
    // Note that count isn't required anymore as we're using
    // ValuesAdd.Length as the count
    NestedForeachRecursive(string.Empty, 0);
}

public void NestedForeachRecursive(string prefix, int depth)
{
    foreach (var item in ValuesAdd[depth])
    {
        var nextDepth = depth + 1;
        var nextPrefix = prefix + item;

        if (nextDepth < ValuesAdd.Length)
            NestedForeachRecursive(nextPrefix, nextDepth);
        else
            Console.WriteLine(nextPrefix);
    }
}

请注意,因为您正在为每个其他项目迭代每个项目,因此其性能将非常差。

此示例的输出为:

a1x
a1y
a1z
a2x
a2y
a2z
a3x
a3y
a3z
b1x
b1y
b1z
b2x
... and so on
相关问题