2011-02-01 10 views
1

私はページ上のこの選択を持っている:Selenium 2 + WebDriver + .NET:selectが "display:none;"の場合、selectのオプションのテキストを取得できません。

<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コレクション内のすべての要素が空の文字列を持っているため、2番目のアサートが失敗していますそれらのTextオブジェクトdisplay:none;スタイルを追加したページでJavaScriptを削除すると動作します。これは、永続的な解決策ではありません。この選択は、FCBKcompleteによって拡張されているため、非表示にする必要があります。

.NETのSelenium 2/WebDriverで隠しセレクトオプションのテキストを取得するにはどうすればよいですか?

答えて

2

WebDriverは、実際のユーザーのやりとりをエミュレートするように設計されています。何かが見えない場合、実際のユーザーはそれを見ることができず、WebDriverはそれを見ることもできません。

ユーザーの操作をエミュレートすることができます。ユーザーの操作をエミュレートすることができます。選択したオプションを見つけて調べることができます。

+0

これは意味があります。 FCBKCompleteがレンダリングするオートコンプリートドロップダウンから情報を見つける方法を理解する必要があります。ありがとう! – adamjford

+0

また、表示プロパティを変更してテキストを取得するJavaScriptを実行することもできます。スクリプトを実行するために、IWebDriverをIJavascriptExecutorにキャストする(私はJavaを使用しているので、インタフェース名に間違いがあるかもしれません) –

1

同じ問題が発生していました。私は、すべての要素を取得してループした場合、どの要素が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++; 
    } 
} 
関連する問題