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;
@Test
public void method46()
{
driver=new FirefoxDriver();
driver.get(“Your url”);
driver.manage().window().maximize();
WebElement element1= driver.findElement(By.xpath(“element xpath”));
//scolling to the webelement
scrolltoElement(element1);
//highlight that element
highlightelement(element1);
//performing clicking event
element1.click();
}
//method to scroll
public static void scrolltoElement(WebElement ScrolltoThisElement) {
Coordinates coordinate = ((Locatable) ScrolltoThisElement)
.getCoordinates();
coordinate.onPage();
coordinate.inViewPort();
}
//method to highlight
public static void highlightelement(WebElement element) {
for (int i = 0; i < 4; i++) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(
“arguments[0].setAttribute(‘style’, arguments[1]);”,
element, “color: yellow; border: 4px solid blue;”);
js.executeScript(
“arguments[0].setAttribute(‘style’, arguments[1]);”,
element, “”);
}
}
}

Advertisements

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 …

    Like

    • charan163 June 5, 2015 / 5:30 pm

      ThankYou !

      Like

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

    Awesum

    Like

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

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

    Like

    • charan163 June 14, 2015 / 6:46 pm

      lol It Works Amit.Try It !!!

      Like

  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

    Like

    • 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.

      Like

  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.

    Like

    • charan163 June 15, 2015 / 2:49 pm

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

      Like

  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

    Like

    • 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.

      Like

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

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

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s