Thursday, 31 March 2016

How to click on WebElements in Selenium using JavaScript

Agenda of this blog: To discuss about how to click on any Web Element 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 code snippet which performs the click operation on webelement using Javascript.


public void javaScriptClick(WebDriver driver,By locator){
                        try{
                        WebElement element=driver.findElement(locator);
                        ((JavascriptExecutor)driver).executeScript("arguments[0].click();", element);
                        }
                        catch(NoSuchElementException ex){
                       
                        Assert.fail("Element " + locator + " was not found in HTML DOM- NoSuchElementException"+ ex.getMessage());
                       
                        }
                       
                        catch(Exception e){
                                    Assert.fail("Exception occured : "+e.getMessage());
                        }
            }

            
Visit for More Automation Related Discussion:


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





No comments:

Post a Comment