如何使用Selenium WebDriver C#从下拉菜单中select一个选项?

我正在尝试为我的networkingtestingselect一个选项。 一个例子可以在这里find: http : //www.tizag.com/phpT/examples/formex.php

除了select一个选项部分,一切都很好。 如何按价值或标签select一个选项?

我的代码:

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 } } 

您必须从下拉列表中创build一个select元素对象。

  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"); 

更多信息在这里

其他方式可能是这样的:

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

你可以改变选项[x]中的索引,将x改变为你想要select的元素的数量。

我不知道这是不是最好的方法,但我希望能帮到你。

通过文本select选项;

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

通过值select一个选项:

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

为此添加一个点。 在将Selenium.NET绑定安装到C#项目后,我发现OpenQA.Selenium.Support.UI命名空间不可用。 后来发现我们可以通过在NuGet包pipe理器控制台中运行命令Install-Package Selenium来支持最新版本的Selenium WebDriver支持类。

这是如何工作对我来说(select控制由ID和文本选项):

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

使用:

 clickOptionInList("ctl00_ContentPlaceHolder_lbxAllRoles", "Tester"); 

Selenium WebDriver从下拉列表中select项目的C#代码:

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

有三种方法select下拉项目:i)按文字selectii)按索引selectiii)按值select

按文字select:

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

按索引select:

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

按值select:

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

您只需要传递值并input密钥:

 driver.FindElement(By.Name("education")).SendKeys("Jr.High"+Keys.Enter); 

如果您正在寻找下拉框中的任何选项,我也会发现“按索引select”方法非常有用。

 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); } 
  var select = new SelectElement(elementX); select.MoveToElement(elementX).Build().Perform(); var click = ( from sel in select let value = "College" select value );