How to select an option from drop down using Selenium WebDriver C#?

匿名 (未验证) 提交于 2019-12-03 08:36:05

问题:

I was trying for my web test selecting an option. An example can be found here: http://www.tizag.com/phpT/examples/formex.php

Everything works great except the selecting an option part. How to selecting an option by value or by label ?

My Code:

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

回答1:

You must create a select element object from the drop down list.

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

More info here



回答2:

Other way could be this one:

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

and you can change the index in option[x] changing x by the number of element that you want to select.

I don't know if it is the best way but I hope that help you.



回答3:

Adding a point to this. I came across a problem that OpenQA.Selenium.Support.UI namespace was not available after installing Selenium.NET binding into the C# project. Later found out that we can easily install latest version of Selenium WebDriver Support Classes by running the command Install-Package Selenium.Support in NuGet Package Manager Console.



回答4:

To Select an Option Via Text;

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

To Select an Option via Value:

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


回答5:

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_Cont  
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!