99

我正在尝试为我的网络测试选择一个选项。一个例子可以在这里找到: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

您必须从下拉列表中创建一个选择元素对象。

 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

补充一点——我遇到了一个问题,即在将 Selenium.NET 绑定安装到 C# 项目后,OpenQA.Selenium.Support.UI 命名空间不可用。后来发现我们可以通过运行以下命令轻松安装最新版本的 Selenium WebDriver Support Classes:

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

用于从下拉菜单中选择项目的 Selenium WebDriver C# 代码:

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

This is how it works for me (selecting control by ID and option by text):

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

use:

clickOptionInList("ctl00_ContentPlaceHolder_lbxAllRoles", "Tester");
于 2013-03-05T22:44:35.957 回答
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

如果您只是从下拉框中寻找任何选择,我还发现“按索引选择”方法非常有用。

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
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 回答