I'm using the Selenium IDE to record my initial tests and then export them to c# and then use the webdriver. Some things that I do from the IDE don't export.
I am trying to select a State from a drop-down text list. I don't have much background in HTML and am kind of new to C# also (I've programmed for a couple semesters in Java so far, c# for about a month.) How would I go about selecting a specific state from the dropdown list?
-
I felt it wasn't as easy to get to the Webdriver documentation for the C#, but I think spending some time here may help you: selenium.googlecode.com/git/docs/api/dotnet/index.htmlSuchit Parikh– Suchit Parikh2013年08月12日 21:21:53 +00:00Commented Aug 12, 2013 at 21:21
7 Answers 7
I'm assuming you're using Firefox? Right-click the drop-down list and select "Inspect Element". Get the tag from the information (everything in green following the pound sign). Then add this to your code where you need to select the element
new SelectElement(driver.FindElement(By.Id("{put your tag information here}"))).SelectByText("{State here}");
Added to Darain, use Xpath(absolute) for locate element. To get that, use fire path(with fire bug). You will get a xpath for each element.Fire path provides Xpath accurately(some time I get xpath is not working which I get from IDE). You can maintain your own array to get element from drop down by recognizing absolute xpath string. Best idea is to get all elements at a time and use particular one when you need.
I am using Google Chrome browser and performing operations on the Facebook register page. So I want to select Birth Month from "month" dropdown list.
SelectElement ss = new SelectElement(driver.FindElement(By.Id("month")));
Console.WriteLine(ss.Options);
foreach(IWebElement element in ss.Options)
{
if(element.Text == "Jun")
{
element.Click();
}
}
You can use one single line to select the dropdown option. Avoid any extra line/code to click the dropdown.
driver.FindElement(By.Id("DropdownId")).FindElement(By.XPath(".//option[contains(text(),'OptionText')]")).Click();
-
For an answer to an old question that's already got an accepted answer, you really need to do more than give a single line of code as your answer.Kate Paulk– Kate Paulk2014年09月30日 10:44:02 +00:00Commented Sep 30, 2014 at 10:44
-
@KatePaulk Yeah. You are right. I will take care from now on. :)ViPuL5– ViPuL52014年09月30日 15:46:31 +00:00Commented Sep 30, 2014 at 15:46
I'm using c# with selenium. I prefer not to use xpaths but tend to package up my most commonly used methods. This one will allow me to select from a droplist (assuming the devs have done their job as expected and put IDs on both the droplist and the elements within the droplist):
public static SelectElement ActionGetSelectElementById(this WebSiteBase unitTest, string id)
{
IWebElement elem = FindElementById(unitTest, id);
if (elem.TagName != "select")
{
throw new InvalidCastException("Element is not a select element, ID: " + id);
}
return new SelectElement(elem);
}
First you have to locate the web-element uniquely presented as select list on UI, you can use Firebug for that to find list element's id, name, xpath or css path. Then create SelectElement object passing this element locator attribute. To select an option in the list, you can choose any option like SelectByValue or SelectByText.
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
class SelectingOption
{
static void Main(string[] args)
{
IWebDriver driver = new FirefoxDriver(); //Launch Firefox browser
driver.Navigate().GoToUrl("http://www.abc.com/"); //Go to application URL
//Authentication and navigation code to page where select list there
SelectElement se = new SelectElement(driver.FindElement(By.id("select_element_id"))); //Locating select list
se.SelectByText("Item1"); //Select item from list having option text as "Item1"
//se.SelectByValue("Item1"); //Select item from list having option value as "Item1"
//Following code to selecting item from list
driver.Quit(); //Quitting the Firefox
}
}
internal void SelectFromDropDown(string selectListOption)
{
SelectElement selectElement = new SelectElement(driver.FindElement(By.Id("elementId")));
selectElement.Options.ToList().Find(x => x.Text == selectListOption).Click();
}
Re-usable
internal void SelectFromDropDown(string elementId,string selectListOption)
{
SelectElement selectElement = new SelectElement(driver.FindElement(By.Id($"{elementId}")));
selectElement.Options.ToList().Find(x => x.Text == selectListOption).Click();
}