Thursday, 31 March 2016

Hightlighting or focusing a web element in Selenium using Javascript

Agenda of this blog: To discuss about how to highlight/focus an web element using using JavaScript in Selenium WebDriver.

What is the need: Sometimes its very useful to highlight webelement before performing any web-actions on it so that we can get an idea on which element we are performing the specific action.

Here is the code snippet which high lights a web element field using Javascript.



           
                                   
            public static void setHighlight(WebElement element)
            {
                                    String attributeColourvalue = "border:3px solid green";
                                    JavascriptExecutor executor = (JavascriptExecutor) driver;
                                    String getAttribute = element.getAttribute("style");
                                    executor.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, attributeColourvalue);
                                    try
                                    {
                                                Thread.sleep(100);
                                    }
                                    catch (InterruptedException e)
                                    {
                                                System.out.println("Sleep interrupted - "+e);
                                    }
                                    executor.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, getAttribute);
                             
            }


Visit for More Automation Related Discussion:

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






No comments:

Post a Comment