How to Scroll to the Particular WebElement and Highlight it in Selenium

This post tells you, How to Scroll to the particular WebElement and Highlight it.In General we all wanted to know, on which element an action is going on.So, by scrolling to it and highlighting it solves the issue.
Just call the two methods ‘scrolltoElement’ & ‘highlightelement’ where ever you need.Keep all these methods in a Utils class and call whenever you need !!

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.internal.Coordinates;
import org.openqa.selenium.internal.Locatable;
import org.testng.annotations.Test;
public class Test123 {
WebDriver driver;
public void method46()
driver=new FirefoxDriver();
driver.get(“Your url”);
WebElement element1= driver.findElement(By.xpath(“element xpath”));
//scolling to the webelement
//highlight that element
//performing clicking event;
//method to scroll
public static void scrolltoElement(WebElement ScrolltoThisElement) {
Coordinates coordinate = ((Locatable) ScrolltoThisElement)
//method to highlight
public static void highlightelement(WebElement element) {
for (int i = 0; i < 4; i++) {
JavascriptExecutor js = (JavascriptExecutor) driver;
“arguments[0].setAttribute(‘style’, arguments[1]);”,
element, “color: yellow; border: 4px solid blue;”);
“arguments[0].setAttribute(‘style’, arguments[1]);”,
element, “”);

13 thoughts on “How to Scroll to the Particular WebElement and Highlight it in Selenium

  1. Mousam June 5, 2015 / 5:10 pm

    Good job …


    • charan163 June 5, 2015 / 5:30 pm

      ThankYou !


  2. Anonymous June 6, 2015 / 12:06 am



  3. amit June 14, 2015 / 4:24 pm

    not yet tested, if it works , Its very great ……


    • charan163 June 14, 2015 / 6:46 pm

      lol It Works Amit.Try It !!!


  4. Sunny June 15, 2015 / 11:19 am

    Hi Charan,

    thanks for the blog.

    In ScrollElement method, onPage , inViewPoint() methods showing errors asking to cast..

    and Highlightelement arguments is shwoing error.asking to add local var..please help me


    • charan163 June 15, 2015 / 11:25 am

      Sunny, Please give the xpath of an element and Casting is not an issue.And also import javascriptExcecutor.


  5. Sunny June 15, 2015 / 2:08 pm

    I have imported JavaScriptExecutor..still its showing error and coming to Scroll element..can you please elaborate on that where to mention xpath.


    • charan163 June 15, 2015 / 2:49 pm

      Enter element’s xpath in WebElement element1= driver.findElement(By.xpath(“element xpath”));


  6. Sunny June 23, 2015 / 11:01 am

    Thanks charan.highlight element is working fine..but still in scroll element onPage() , inViewPoint() methods showing errors asking to cast..even if casting also still error ..please help me charan


    • charan163 June 23, 2015 / 9:09 pm

      Just mention the webelement in that method.Idk whats wrong the with your code,Try to share it.We could help.


  7. Sunny June 24, 2015 / 8:23 am

    charan please share your email that i can send an email..


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s