9

WebDriver の C# バインディングを使用して、ドロップダウンから値を選択するのに苦労しています。過去に C# も WebDriver も使用したことがありません。Visual Studio C# 2010 Express エディションで WebDriver - Selenium-dotnet2.0b3 を使用しています。WebDriver.Common、WebDriver.Firefox、および WebDriver.Remote をソリューションに追加しました。これを使ってみた -

IWebElement dateOfBirth = webdriver.FindElement(By.Id("join_birth_day"));
List<IWebElement> dateOfBirthOptions = (List<IWebElement>)dateOfBirth.FindElement(By.TagName("option"));

foreach(IWebElement dateOfBirthOption in dateOfBirthOptions)  
{
    if (dateOfBirthOption.Equals("3"))
    {
        dateOfBirthOption.Select();
    }
}

しかし、NUnit で私のソリューションを実行すると、エラーが表示されます

LiveCams.CreateAccount.createAccount:
System.InvalidCastException : Unable to cast object of type 'OpenQA.Selenium.Firefox.FirefoxWebElement' to type 'System.Collections.Generic.List`1[OpenQA.Selenium.IWebElement]'.

キャストしないと、ソリューションを構築することさえできません。ここで些細なことを見逃していると思います。ここで私を案内してくれる人はいますか? ドロップダウン選択は、Selenium 1.0 ではとてもシンプルでした:-/

4

4 に答える 4

10

ドロップダウンからオプションを選択するには、以下のコードを使用します

  1. テキストに基づいて値を選択するには

    new SelectElement(driver.FindElement(By.XPath(""))).SelectByText("");
    
  2. 値に基づいて値を選択するには

    new SelectElement(driver.FindElement(By.XPath(""))).SelectByValue("");
    
  3. インデックスに基づいて値を選択するには

    new SelectElement(driver.FindElement(By.XPath(""))).SelectByIndex(0);
    
于 2015-07-08T11:48:12.440 に答える
7

1) 既にコメントされているように SelectElement を使用する - Selenium WebDriver C# を使用してドロップダウンからオプションを選択する方法は? SelectElement はOpenQA.Selenium.Support.UI名前空間に属します。

2) CSS セレクターを使用して、次のようなこともできます。

WebElement dateOfBirth =  webdriver.FindElement(By.Id("join_birth_day"))
                              .FindElement(By.CssSelector("option[value='3']")).Select();
于 2011-03-30T09:42:54.260 に答える
4

OpenQA.Selenium.Support.UI 名前空間で定義されている次のクラス SelectElement を使用します。この単語Selectは C# で既に使用されているため、実装が変更され、クラスの名前が異なります。

    // Summary:
    //     Initializes a new instance of the SelectElement class.
    //
    // Parameters: element - The element to be wrapped
    //
    public SelectElement(IWebElement element);

このクラスのオブジェクトを作成すると、インデックス、テキスト、および値に基づいて選択するオプションがあります。

    // Summary:
    //     Select the option by the index, as determined by the "index" attribute of
    //     the element.
    //
    // Parameters:
    //   index:
    //     The value of the index attribute of the option to be selected.
    public void SelectByIndex(int index);

    // Summary:
    //     Select all options by the text displayed.
    //
    // Parameters:
    //   text:
    //     The text of the option to be selected. If an exact match is not found, this
    //     method will perform a substring match.
    // Remarks:
    //     When given "Bar" this method would select an option like:
    //     <option value="foo">Bar</option>
    public void SelectByText(string text);

    // Summary:
    //     Select an option by the value.
    //
    // Parameters:
    //   value:
    //     The value of the option to be selected.
    // Remarks:
    //     When given "foo" this method will select an option like:
    //     <option value="foo">Bar</option>
    public void SelectByValue(string value);
于 2014-05-14T12:20:37.067 に答える