0

I have a combo box with 2 values(say Xreg and MBA). By default only one value(either Xreg or MBA) will be displayed based on a search criteria.. The xpath for Xreg is

/html/body/div/div[4]/div[2]/form/div[1]/table[2]/tbody/tr[1]/td[2]/select/option[2] 

and for MBA

/html/body/div/div[4]/div[2]/form/div[1]/table[2]/tbody/tr[1]/td[2]/select/option[3]

How do i capture the default value on page load. It might be either of them and each time i want to capture the value that is displayed by default in the combo box

drkthng
  • 6,651
  • 7
  • 33
  • 53
  • can you please share HTML code of that combo box – Shubham Jain Sep 16 '15 at 07:31
  • Welcome to Stack Overflow! Please read the guide [How do I ask a good question](http://stackoverflow.com/help/how-to-ask), especially the part on Minimal, Complete, and Verifiable example (MCVE). This will help you solve problems for yourself. If you do this and are still stuck you can come back and post your MCVE, what you tried, and what the results were so we can better help you. – JeffC Sep 17 '15 at 04:11

1 Answers1

1

You can use Selenium's Select class for this:

// this is only an example with the code provided, usually the select element has an id and you wouldn't necessarily need xpath
By locatorToYourSelectElement = By.xpath("/html/body/div/div[4]/div[2]/form/div[1]/table[2]/tbody/tr[1]/td[2]/select");

WebElement selectElement = driver.findElement(locatorToYourSelectElement);
Select dropdown = new Select(selectElement);

// Supposing you do not have multiple selection you will get the displayed element now very easily:
WebElement currentlySelectedOption = dropdown.getFirstSelectedOption();
drkthng
  • 6,651
  • 7
  • 33
  • 53