2

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?

1

7 Answers 7

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

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.

1

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();
      }
 }
0

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();
2
  • 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
    Sep 30, 2014 at 10:44
  • @KatePaulk Yeah. You are right. I will take care from now on. :)
    – ViPuL5
    Sep 30, 2014 at 15:46
0

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);
    }
0

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
    }
}
0
 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();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.