99

オプションを選択してWebテストを試みていました。例はここにあります:http ://www.tizag.com/phpT/examples/formex.php

オプション部分の選択を除いて、すべてがうまく機能します。値またはラベルでオプションを選択するにはどうすればよいですか?

私のコード:

using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
using System.Threading;
using System.Diagnostics;
using System.Runtime.InteropServices;

class GoogleSuggest
{
    static void Main()
    {
        IWebDriver driver = new FirefoxDriver();

        //Notice navigation is slightly different than the Java version
        //This is because 'get' is a keyword in C#
        driver.Navigate().GoToUrl("http://www.tizag.com/phpT/examples/formex.php");
        IWebElement query = driver.FindElement(By.Name("Fname"));
        query.SendKeys("John");
        driver.FindElement(By.Name("Lname")).SendKeys("Doe");
        driver.FindElement(By.XPath("//input[@name='gender' and @value='Male']")).Click();
        driver.FindElement(By.XPath("//input[@name='food[]' and @value='Chicken']")).Click();
        driver.FindElement(By.Name("quote")).Clear();
        driver.FindElement(By.Name("quote")).SendKeys("Be Present!");
        driver.FindElement(By.Name("education")).SendKeys(Keys.Down + Keys.Enter); // working but that's not what i was looking for
        // driver.FindElement(By.XPath("//option[@value='HighSchool']")).Click(); not working
        //  driver.FindElement(By.XPath("/html/body/table[2]/tbody/tr/td[2]/table/tbody/tr/td/div[5]/form/select/option[2]")).Click(); not working
        // driver.FindElement(By.XPath("id('examp')/x:form/x:select[1]/x:option[2]")).Click(); not working

        }
}
4

10 に答える 10

204

ドロップダウンリストからselectelementオブジェクトを作成する必要があります。

 using OpenQA.Selenium.Support.UI;

 // select the drop down list
 var education = driver.FindElement(By.Name("education"));
 //create select element object 
 var selectElement = new SelectElement(education);

 //select by value
 selectElement.SelectByValue("Jr.High"); 
 // select by text
 selectElement.SelectByText("HighSchool");

詳細はこちら

于 2011-03-14T08:37:28.450 に答える
16

これにポイントを追加-C#プロジェクトにSelenium.NETバインディングをインストールした後、OpenQA.Selenium.Support.UI名前空間が使用できないという問題に遭遇しました。後で、次のコマンドを実行することで、最新バージョンのSeleniumWebDriverサポートクラスを簡単にインストールできることがわかりました。

Install-Package Selenium.Support

NuGetパッケージマネージャーコンソールで、またはNuGetマネージャーからSelenium.Supportをインストールします。

于 2015-02-21T04:50:46.777 に答える
10

他の方法はこれである可能性があります:

driver.FindElement(By.XPath(".//*[@id='examp']/form/select[1]/option[3]")).Click();

また、option [x]でインデックスを変更し、選択する要素の数だけxを変更できます。

それが最善の方法かどうかはわかりませんが、お役に立てば幸いです。

于 2012-05-25T23:13:46.633 に答える
3

テキストを介してオプションを選択するには;

(new SelectElement(driver.FindElement(By.XPath(""))).SelectByText("");

値を介してオプションを選択するには:

 (new SelectElement(driver.FindElement(By.XPath(""))).SelectByValue("");
于 2015-04-27T12:13:04.730 に答える
3

ドロップダウンからアイテムを選択するためのSeleniumWebDriverC#コード:

IWebElement EducationDropDownElement = driver.FindElement(By.Name("education"));
SelectElement SelectAnEducation = new SelectElement(EducationDropDownElement);

ドロップダウンアイテムを選択するには、次の3つの方法があります。i)テキストで選択ii)インデックスで選択iii)値で選択

テキストで選択:

SelectAnEducation.SelectByText("College");//There are 3 items - Jr.High, HighSchool, College

インデックスで選択:

SelectAnEducation.SelectByIndex(2);//Index starts from 0. so, 0 = Jr.High 1 = HighSchool 2 = College

値で選択:

SelectAnEducation.SelectByValue("College");//There are 3 values - Jr.High, HighSchool, College
于 2017-05-23T13:34:21.040 に答える
2

値を渡してキーを入力するだけです。

driver.FindElement(By.Name("education")).SendKeys("Jr.High"+Keys.Enter);
于 2014-07-30T07:21:10.620 に答える
1

これは私にとってどのように機能するかです(IDによるコントロールとテキストによるオプションの選択):

protected void clickOptionInList(string listControlId, string optionText)
{
     driver.FindElement(By.XPath("//select[@id='"+ listControlId + "']/option[contains(.,'"+ optionText +"')]")).Click();
}

使用する:

clickOptionInList("ctl00_ContentPlaceHolder_lbxAllRoles", "Tester");
于 2013-03-05T22:44:35.957 に答える
0

ドロップダウンボックスから選択したものだけを探している場合は、「インデックスで選択」方法も非常に便利です。

if (IsElementPresent(By.XPath("//select[@id='Q43_0']")))
{
    new SelectElement(driver.FindElement(By.Id("Q43_0")))**.SelectByIndex(1);** // This is selecting first value of the drop-down list
    WaitForAjax();
    Thread.Sleep(3000);
}
else
{
     Console.WriteLine("Your comment here);
}
于 2016-12-01T23:53:29.003 に答える
0
 var select = new SelectElement(elementX);
 select.MoveToElement(elementX).Build().Perform();

  var click = (
       from sel in select
       let value = "College"
       select value
       );
于 2017-09-20T09:06:31.477 に答える
0
IWebElement element = _browserInstance.Driver.FindElement(By.XPath("//Select"));
IList<IWebElement> AllDropDownList = element.FindElements(By.XPath("//option"));
int DpListCount = AllDropDownList.Count;
for (int i = 0; i < DpListCount; i++)
{
    if (AllDropDownList[i].Text == "nnnnnnnnnnn")
    {
        AllDropDownList[i].Click();
        _browserInstance.ScreenCapture("nnnnnnnnnnnnnnnnnnnnnn");
    }
}
于 2018-03-15T20:01:01.657 に答える