Saturday, 2 July 2016

Selecting drop down value using JavaScript in Selenium

In some web applications, when we select a value from the drop down, new controls or elements appear or disappear on the webpage. In short, change event is fired. But sometimes, change event does not get fired after selecting the value from drop down using selenium.

In such scenarios, we can use JavascriptExecutor to fire the change event on drop down element as shown in below code.

protected void fireChangeEvent(WebElement element){
        ((JavascriptExecutor) driver).executeScript(" var evt = 
document.createEvent('HTMLEvents'); evt.initEvent('change',true,true); 
arguments[0].dispatchEvent(evt);",element);
    }

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