Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从下拉列表中选择随机选项_C#_Selenium_Selenium Webdriver - Fatal编程技术网

C# 从下拉列表中选择随机选项

C# 从下拉列表中选择随机选项,c#,selenium,selenium-webdriver,C#,Selenium,Selenium Webdriver,我需要从下拉列表中选择一个随机选项。我的理解是,我需要计算可用选项的数量,然后选择一个介于0和可用选项数量之间的随机索引 到目前为止,我有这个- public static void selectRandomIndexDropdown(this IWebDriver driver, By elementName) { var element = driver.FindElement(elementName); element.selectRandomIndexDropdown()

我需要从下拉列表中选择一个随机选项。我的理解是,我需要计算可用选项的数量,然后选择一个介于0和可用选项数量之间的随机索引

到目前为止,我有这个-

public static void selectRandomIndexDropdown(this IWebDriver driver, By elementName)
{
    var element = driver.FindElement(elementName);
    element.selectRandomIndexDropdown();
}

public static void selectRandomIndexDropdown(this IWebElement element)
{
    if (element == null || element.TagName.ToLower() != "select")
        return;

    int indexCount = element.FindElements(By.TagName("option")).Count();
    new SelectElement(element).SelectByIndex(Rnd.Next(0, indexCount));
}
并试着称之为-

driver.selectRandomIndexDropdown(By.XPath("//*[@id='ASPxGridViewDeskFees_DXEFL_DXEditor1_I']"));
这似乎不起作用。没有显示错误或抛出异常,看起来代码似乎已执行,但只是没有单击列表中的某个选项。有什么想法吗?

我正在使用Java,但我相信C语言的答案是类似的

我通常使用一种方法提供当前可用选项列表:

public static List<WebElement> getOptions(WebElement selectField) {
    Select dropdown = new Select(selectField);
    return dropdown.getOptions();
}

或者简单地从2生成randum X nuber,到选项数,然后查找如下元素:

string myxpath = "//*[@id='select_id']/option[" + X + "]";
IWebElement element = driver.FindElement(By.XPath(myxpath));

展开-似乎不工作,请。添加了更多的细节,基本上代码执行,但没有选择任何选项。没有引发错误或异常。在哪里选择随机数?除息的
string myxpath = "//*[@id='select_id']/option[" + X + "]";
IWebElement element = driver.FindElement(By.XPath(myxpath));