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.

Following code works for selecting the last item from dropdown using Selenium 2.28 driver on Firefox and IE. Not working with Selenium Web Driver 2.40. I can see the dropdown opening but it is not responding selecting the option by using selectByIndex. How do I make it work? I am testing on Firefox 28 and IE8 on Windows 7.

WebElement dropDown = WebDriver.findElement(By.css("select name='tblCampaigns_length']"));
Select select = new Select(dropDown);
select.selectByIndex(select.getOptions().size()-1);
share|improve this question
    
are you getting any exceptions or other errors? Where is it failing at? –  Dan Snell Apr 11 at 15:53

1 Answer 1

try the following: c#

IWebElement dropDown = WebDriver.findElement(By.css("select name='tblCampaigns_length']"));
SelectElement select = new SelectElement(dropDown);
select.selectByIndex(select.Options.Count - 1);

The .Options retruns a list of options that then inherits the .Count in being a list. This will return the exact number of option elements that belong to the SelectElement object. It is 1 incremented as a count instead of 0 and therefore the -1 is added to sync up the correct index selection to the last one in the list.

share|improve this answer
    
Could you add some explanation for why this would work for the OP? We prefer answers to have more context than just the code. –  Kate Paulk Aug 8 at 10:44

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.