我在页面上有这样的选择:
<select multiple="" class="recipientsList" name="Recipients[]" id="To" style="display: none;">
<option value="User-6" class="">Coordinator, Test</option>
<option value="Course-4" class="">New Course 1</option>
<option value="UserType-6" class="">Coordinators</option>
<option value="UserTypeInCourse-4-6" class="">New Course 1 Coordinator</option>
</select>我正在做这个测试:
public IWebDriver WebDriver
{
get
{
// gets the current WebDriver instance, set up elsewhere at the beginning
// of the fixture
return ScenarioContext.Current.WebDriver();
}
}
public void SelectTest()
{
// code to navigate to proper page
var options = WebDriver.FindElements(By.CssSelector("select.recipientsList option"));
Assert.That(options, Is.Not.Empty, "No options found.");
Assert.That(!options.Any(option => string.IsNullOrEmpty(option.Text)), "Some or all options have blank text.");
// Actual useful assert
}第二个断言失败了,因为options集合中的所有元素都有空字符串作为文本对象。如果我删除添加display:none;样式的页面上的display:none;,它就能工作。但是,这不是一个永久的解决方案,因为这个选择需要隐藏,因为它是由FCBKcomplete扩展的。
如何在.NET?中使用Selenium 2/WebDriver获取隐藏选择选项的文本
发布于 2011-02-04 19:25:49
WebDriver旨在模拟真实的用户交互。如果某个东西是不可见的,那么真正的用户就看不到它,WebDriver也看不到它。
您可以模仿用户的操作-单击、悬停或任何使您的选择可见-然后找到您的选择的选项并检查它们。
发布于 2011-04-06 12:32:03
我也遇到了同样的问题。我发现,如果我检索了所有的元素并遍历它们,我就可以确定哪些元素被JS或CSS设置为显示,然后与它们交互。
我有一个表单字段,其名称与附加了一个动态id的名称相同,比如"fieldname_"+id作为字段ID。下面是示例代码:
List<WebElement> displayNames = driver.findElements(By.xpath("//input[starts-with(@id, 'calendarForm_calendarDisplayNameM')]"));
int name_count = 1;
for (WebElement thisDisplayName : displayNames) {
RenderedWebElement element = (RenderedWebElement)thisDisplayName;
if (element.isDisplayed()) {
String calendarDisplayNameText = testCalendarName + "_display_" + name_count;
thisDisplayName.sendKeys(calendarDisplayNameText);
name_count++;
}
}https://stackoverflow.com/questions/4865882
复制相似问题