Monday, 3 November 2014

Unable to select value from drop down in selenium webdriver.

Sometimes you may encounter a scenario where selenium Select class can not be used to select the value from the drop down or list box in any browser like internet explorer, google chrome or firefox.
Sometime situation is more difficult because after selecting the value from drop down, other controls on the page get auto populated.
So basically there are 2 problems.
  1. Selenium unable to select the value from the drop down.
  2. Java script event does not get invoked. So other controls do not get auto populated.
We can solve above issues by below code.

WebElement c = driver.findElement(By.xpath("//select[@name='empType']"));
((JavascriptExecutor) driver).executeScript("arguments[0].value='X';",c);

Above script will select value X from the drop down.

After this you will have to trigger the on change event on the drop down using below code.

((JavascriptExecutor) driver).executeScript(
"var evt = document.createEvent('HTMLEvents');
evt.initEvent ('change', true, true);
arguments[0].dispatchEvent(evt);",c);


This will fire the drop down change event on the web list.
What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

Contributors