1

Using Selenium Webdriver (Java)

I need to verify that a selected value from a dropdown menu is saved when a user returns to that page in another session.

The dropdown is simple, it just changes the number of records displayed per page after a search.

The values are 5, 10, 25, 50 and 100.

Using getText() simply returns the values in the array. I need to verify that if a user chooses, say, 25, that that value is the same when returning to this particular page. A simple assert statement doesn't work here because the value "25" can be present in other fields.

I've also tried various uses of Select without success.

I'd appreciate any suggestions. If I've missed any key information, feel free to point it out and I will update the post.

John McGrath
  • 11
  • 1
  • 1
  • 3

3 Answers3

4

You need to use Select class

IWebElement selectElement = driver.FindElement(By.Id("id"));
SelectElement selectedValue = new SelectElement(selectElement);
string selectedText = selectedValue.SelectedOption.Text;

Mine written in C#. But Java is fairly close as well.See this

EDIT: you should be using getFirstSelectedOption().getText() instead of SelectedOption.Text; according to the api doc

Saifur
  • 16,081
  • 6
  • 49
  • 73
1

checkout the following answer

Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down")));
WebElement option = select.getFirstSelectedOption()
Community
  • 1
  • 1
deepak
  • 3,074
  • 2
  • 20
  • 29
  • Thanks for the feedback. I did as you suggested and it sort of did what I needed. Select select = new Select(driver.findElement(By.xpath("//*[@id='listingDetailJqGrid_toppager_center']/table/tbody/tr/td[8]/select"))); WebElement option = select.getFirstSelectedOption(); Now it returns the actual element properties. The index of the selected option is: [[[[FirefoxDriver: firefox on WINDOWS (4a0c1158-004e-4a48-843e-d2952e4af4ff)] -> xpath: //*[@id='listingDetailJqGrid_toppager_center']/table/tbody/tr/td[8]/select]] -> tag name: option] I was expecting 25 or at least it's index position. – John McGrath Dec 08 '14 at 20:51
  • @JohnMcGrath You need to use `select.getFirstSelectedOption( ).getText( );` in order to get text – Saifur Dec 08 '14 at 21:04
  • Thanks for the help. Haven't been doing this very long beyond basic asserts. Now I'm being asked to provide more detailed tests and I don't have a whole lot of experience. This was what I needed. Very much appreciated, Saifur. – John McGrath Dec 09 '14 at 01:57
0

-- This will give the selected option in dropdown

Select select = new Select(driver.findElement(By.xpath("//path_to_drop_down"))); WebElement option = select.getFirstSelectedOption() String SelectedText = option.getText();

Pradeep
  • 21
  • 2