Saturday, April 18, 2020

Added Text in label using JavascriptExecutor in Selenium WebDriver

Here we will discuss how we can put some text in html field. It is require to add some data within html field.

Steps: 1 

                JavascriptExecutor executor = (JavascriptExecutor)driver;

 Steps: 2

            executor.executeScript("arguments[0].innerHTML = '" + text +"';", element);

Here we set the innerHTML value and added some extra text.


public static void addText(WebElement element, String text) {
        try
        {
            JavascriptExecutor executor = (JavascriptExecutor)driver;
            executor.executeScript("arguments[0].innerHTML = '" + text +"';", element);
        }
        catch (Exception e)
        {
            log.error("Couldnot able to send text on element: " + element);
            throw e;
        }
    }

No comments:

Post a Comment