如何等待直到下拉选择的文本与使用硒的其他文本匹配?
问题描述:
HTML:
<select name="ddlFruit" id="ddlFruit" class="Searchddl">
<option value="">Select</option>
<option value="447">Grapes</option>
<option value="448">Mango</option>
<option selected="selected" value="449">Apple</option>
</select>
假设"Apple"处于首选模式,由于站点上还有其他操作,此下拉列表会自动更改为其他选项.我希望网络驱动程序等到"Mango"文本处于选定模式.
Suppose "Apple" is in first selected mode, due to some other actions on site, this drop-down changes to other options automatically. I want webdriver to wait until "Mango" text is in selected mode.
尝试的代码:
public static SelectElement FindSelectElementWhenPopulated(IWebDriver driver, By by, int delayInSeconds, string optionText)
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(delayInSeconds));
return wait.Until<SelectElement>(drv =>
{
SelectElement element = new SelectElement(drv.FindElement(by));
if (element.SelectedOption.ToString().Contains(optionText))
{
return element;
}
return null;
}
);
}
Myclass.FindSelectElementWhenPopulated(driver, By.CssSelector("#ddlFruit"), 20, "Mango");
我正在使用C#.
答
您不想将SelectedOption
转换为字符串.改为测试Text
属性:
You don't want to convert the SelectedOption
to a string. Test the Text
property instead:
if (element.SelectedOption.Text.Contains(optionText))
进行一些更改,您可以使其成为WebDriverWait上的便捷扩展方法:
With a few changes, you can make this a handy extension method on WebDriverWait:
public static SelectElement UntilOptionIsSelected(this WebDriverWait wait, By by, string optionText)
{
return wait.Until<SelectElement>(driver =>
{
var element = new SelectElement(driver.FindElement(by));
if (element.SelectedOption.Text.Contains(optionText))
{
return element;
}
return null;
});
}
并使用它:
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
var dropdown = wait.UntilOptionIsSelected(By.CssSelector("#ddlFruit"), "Mango");