5

Is there a way to highlight text entered in the field using sendKey() in Selenium WebDriver in Java?

My code:

public static WebDriver driver, high;
    public static void highlight(WebDriver driver, WebElement ele) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].setAttribute('style', 'background: grey; border: 2px solid yellow;');", ele);
    }
        public static void highlight1(WebDriver high, WebElement ele1) {    
        JavascriptExecutor js1 = (JavascriptExecutor) high;
        js1.executeScript("arguments[0].setAttribute('style', 'background: blue; border: 2px solid yellow;');", ele1);
    }

    public static void main(String[] args) throws InterruptedException  {
        System.setProperty("webdriver.chrome.driver","/home/sharukh/Downloads/chromedriverv80/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS) ;
        driver.get("https://www.mariatash.com/");
        driver.findElement(By.xpath("(//div/div/button[@type='button'])[9]")).click();

        driver.findElement(By.id("sign_in_label")).click();

        WebElement username = driver.findElement(By.id("username-login"));
        highlight(driver,username);
        username.sendKeys("[email protected]");

        WebElement password = driver.findElement(By.id("passwd-login"));
        highlight1(high,password);
        password.sendKeys("QWERTY");

3 Answers 3

2

Did you try:

elem.sendKeys(Keys.CONTROL,"a");

Output

enter image description here

And about your code:

It is failing because you are passing

  WebElement password = driver.findElement(By.id("passwd-login"));
  highlight1(high,password);

Instead of

  WebElement password = driver.findElement(By.id("passwd-login"));
  highlight1(driver,password);

Highlighted output:

enter image description here

2

Use the below function

public static void highlight(WebDriver driver,WebElement element) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);
}

Where driver is your WebDriver instance and element is the WebElement which is to be highlighted.

Sample Code to make google search

package test.cloudblm.com;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class maingoogle {

    public static WebDriver driver;
    public static void highlight(WebDriver driver,WebElement element) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].setAttribute('style', 'background: yellow; border: 2px solid red;');", element);
    }
    public static void main(String[] args) {


        // TODO Auto-generated constructor stub
        System.setProperty("webdriver.chrome.driver",
                new File("yourdriverpath").getAbsolutePath());
        driver = new ChromeDriver();
        driver.get("https://www.google.com/?safe=active&ssui=on");
        WebElement searchbar=driver.findElement(By.xpath("//input[@name=\"q\"]"));
        highlight(driver,searchbar);
        searchbar.sendKeys("QA is fun");


    }

}

Will highlight as shown in image below

enter image description here

2
  • how to use two highlight colors for a different elements.
    – S Sufiyaan
    Mar 17, 2020 at 14:00
  • i have added my code as answer.
    – S Sufiyaan
    Mar 17, 2020 at 14:11
0

THIS IS MY CODE. IN THIS I AM HIGHLIGHTING USERNAME, PASSWORD FIELD.

public static WebDriver driver, high;
    public static void highlight(WebDriver driver, WebElement ele) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("arguments[0].setAttribute('style', 'background: grey; border: 2px solid yellow;');", ele);
    }
        public static void highlight1(WebDriver high, WebElement ele1) {    
        JavascriptExecutor js1 = (JavascriptExecutor) high;
        js1.executeScript("arguments[0].setAttribute('style', 'background: blue; border: 2px solid yellow;');", ele1);
    }

    public static void main(String[] args) throws InterruptedException  {
        System.setProperty("webdriver.chrome.driver","/home/sharukh/Downloads/chromedriverv80/chromedriver");
        WebDriver driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS) ;
        driver.get("https://www.mariatash.com/");
        driver.findElement(By.xpath("(//div/div/button[@type='button'])[9]")).click();

        driver.findElement(By.id("sign_in_label")).click();

        WebElement username = driver.findElement(By.id("username-login"));
        highlight(driver,username);
        username.sendKeys("[email protected]");

        WebElement password = driver.findElement(By.id("passwd-login"));
        highlight1(high,password);
        password.sendKeys("QWERTY");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.