Thursday, 31 March 2016

How to enter text in Selenium using Javascript

Agenda of this blog: To discuss about how to enter text(sendkeys alternative in Selenium) on any web input field using JavaScript in Selenium WebDriver.

What is the need: Sometimes we need to use Javascript in performing some of the web actions as in some cases those web elements does behave sporadically with normal selenium script.

Here is the method which enters the text on web input field using  Javascript.



           
            public void  javaScriptType(WebDriver driver,By locator,String textToBeEntered){
                        try{
                                    WebElement element=driver.findElement(locator);
                                    ((JavascriptExecutor) driver).executeScript("arguments[0].setAttribute('value', '"+textToBeEntered+"');",element);
                        }
                        catch(NoSuchElementException ex){
                                    Assert.fail("Element "+locator+" was not found in DOM "+ex);
                        }
                        catch(Exception ex){
                                    Assert.fail("Exception Occured while entering text using JavaScript: "+ex);
                        }
            }



Visit for More Automation Related Discussion:


https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos






No comments:

Post a Comment