首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Selenium 2+ WebDriver + .NET:如果select为“display:none”,则无法检索select选项的文本;

Selenium 2+ WebDriver + .NET:如果select为“display:none”,则无法检索select选项的文本;
EN

Stack Overflow用户
提问于 2011-02-01 17:30:09
回答 2查看 2.3K关注 0票数 1

我在页面上有这样的选择:

代码语言:javascript
复制
<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>

我正在做这个测试:

代码语言:javascript
复制
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获取隐藏选择选项的文本

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-02-04 19:25:49

WebDriver旨在模拟真实的用户交互。如果某个东西是不可见的,那么真正的用户就看不到它,WebDriver也看不到它。

您可以模仿用户的操作-单击、悬停或任何使您的选择可见-然后找到您的选择的选项并检查它们。

票数 2
EN

Stack Overflow用户

发布于 2011-04-06 12:32:03

我也遇到了同样的问题。我发现,如果我检索了所有的元素并遍历它们,我就可以确定哪些元素被JS或CSS设置为显示,然后与它们交互。

我有一个表单字段,其名称与附加了一个动态id的名称相同,比如"fieldname_"+id作为字段ID。下面是示例代码:

代码语言:javascript
复制
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++;
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4865882

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档