Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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?

share|improve this question
    
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.html –  Suchit Parikh Aug 12 '13 at 21:21

3 Answers 3

up vote 1 down vote accepted

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}");
share|improve this answer

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.

share|improve this answer

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();
share|improve this answer
    
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 at 10:44
    
@KatePaulk Yeah. You are right. I will take care from now on. :) –  ViPuL5 Sep 30 at 15:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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