Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7960039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:39:53+00:00 2026-06-04T04:39:53+00:00

Can someone please help! How can I highlight all web elements in following class

  • 0

Can someone please help!

How can I highlight all web elements in following class during test execution in WebDriver?
With Selenium RC, it was quite straight forward but with WebDriver I am struggling.

I would be grateful if someone can please provide me with some code that I can try, also where would that code fit into the class below — sorry my Java skills aren’t all that great.

package hisScripts;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.*;
import org.testng.Assert;
import static org.testng.Assert.fail;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.interactions.Actions;


public class R_LHAS_Only_Account_Verification extends HIS_Login_Logout{
    public WebDriver driver;
    public String baseUrl;
    public int exeMonth;
    private StringBuffer verificationErrors = new StringBuffer();

    @BeforeClass
    @Parameters ({"browser1", "url", "executionMonth"})
    public void setUp(String browser1, String url, int executionMonth) throws Exception {
        exeMonth = executionMonth;
        baseUrl = url;

        if (browser1.equals("FF")) {
            driver = new FirefoxDriver();
        } else if (browser1.equals("IE")){
            driver = new InternetExplorerDriver();
        }       
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);    
    }       

    @Test
    public void R_LHAS_Reports() throws Exception {
        R_LHAS_Only_Login(baseUrl, driver);
        Assert.assertEquals("Kingston upon Thames (RB)", driver.findElement(By.xpath("//html/body/div[9]/div/div[3]/div/div/div")).getText());
        Assert.assertEquals("Average price", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr/td")).getText());
        Assert.assertEquals("% price change", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[2]/td")).getText());
        Assert.assertEquals("Lower quartile price", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[3]/td")).getText());
        Assert.assertEquals("Time to sell (weeks)", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[4]/td")).getText());
        Assert.assertEquals("% asking price achieved", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[5]/td")).getText());
        Assert.assertEquals("House price to earnings ratio", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[6]/td")).getText());
        Assert.assertEquals("Cost of buying outright - LQ 2 bed £pw", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[7]/td")).getText());
        Assert.assertEquals("Private rent 2 bed £pw", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[8]/td")).getText());
        Assert.assertEquals("80% private rent 2 bed £pw", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[9]/td")).getText());
        Assert.assertEquals("Social rent 2 bed £pw", driver.findElement(By.xpath("//table[@id='tableId']/tbody/tr[10]/td")).getText());                 
        R_LHAS_Only_Logout(baseUrl,driver);
    }

    @AfterClass(alwaysRun=true)
    public void tearDown() throws Exception {
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (! "".equals(verificationErrorString)) {
            fail(verificationErrorString);
        }
    }   
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:39:54+00:00Added an answer on June 4, 2026 at 4:39 am

    There is no way to do this in WebDriver (as of v2.21.0). You can try replacing the usual findElement(By) method with an adjusted one that uses JavaScript to highlight the found element:

    // Draws a red border around the found element. Does not set it back anyhow.
    public WebElement findElement(By by) {
        WebElement elem = driver.findElement(by);
        // draw a border around the found element
        if (driver instanceof JavascriptExecutor) {
            ((JavascriptExecutor)driver).executeScript("arguments[0].style.border='3px solid red'", elem);
        }
        return elem;
    }
    

    Now that you got the idea, there’s an improved version that restores the original border of the last element when a new one is found and highlighted:

    // assuming JS is enabled
    private JavascriptExecutor js = (JavascriptExecutor)driver;
    private WebElement lastElem = null;
    private String lastBorder = null;
    
    private static final String SCRIPT_GET_ELEMENT_BORDER;
    private static final String SCRIPT_UNHIGHLIGHT_ELEMENT;
    
    void highlightElement(WebElement elem) {
        unhighlightLast();
    
        // remember the new element
        lastElem = elem;
        lastBorder = (String)(js.executeScript(SCRIPT_GET_ELEMENT_BORDER, elem));
    }
    
    void unhighlightLast() {
        if (lastElem != null) {
            try {
                // if there already is a highlighted element, unhighlight it
                js.executeScript(SCRIPT_UNHIGHLIGHT_ELEMENT, lastElem, lastBorder);
            } catch (StaleElementReferenceException ignored) {
                // the page got reloaded, the element isn't there
            } finally {
                // element either restored or wasn't valid, nullify in both cases
                lastElem = null;
            }
        }
    }
    

    And the scripts! I load them from a file using FileUtils.readFileToString().

    SCRIPT_GET_ELEMENT_BORDER (IE friendly version taken from this site), it would be way shorter if it used highlighting via changing the background color, say, only the bottom border. But this is the nicest one :).

    /*
     * Returns all border properties of the specified element as String,
     * in order of "width style color" delimited by ';' (semicolon) in the form of:
     * 
     * "2px inset #000000;2px inset #000000;2px inset #000000;2px inset #000000"
     * "medium none #ccc;medium none #ccc;1px solid #e5e5e5;medium none #ccc"
     * etc.
     */
    var elem = arguments[0]; 
    if (elem.currentStyle) {
        // Branch for IE 6,7,8. No idea how this works on IE9, but the script
        // should take care of it.
        var style = elem.currentStyle;
        var border = style['borderTopWidth']
                + ' ' + style['borderTopStyle']
                + ' ' + style['borderTopColor']
                + ';' + style['borderRightWidth']
                + ' ' + style['borderRightStyle']
                + ' ' + style['borderRightColor']
                + ';' + style['borderBottomWidth']
                + ' ' + style['borderBottomStyle']
                + ' ' + style['borderBottomColor']
                + ';' + style['borderLeftWidth']
                + ' ' + style['borderLeftStyle']
                + ' ' + style['borderLeftColor'];
    } else if (window.getComputedStyle) {
        // Branch for FF, Chrome, Opera
        var style = document.defaultView.getComputedStyle(elem);
        var border = style.getPropertyValue('border-top-width')
                + ' ' + style.getPropertyValue('border-top-style')
                + ' ' + style.getPropertyValue('border-top-color')
                + ';' + style.getPropertyValue('border-right-width')
                + ' ' + style.getPropertyValue('border-right-style')
                + ' ' + style.getPropertyValue('border-right-color')
                + ';' + style.getPropertyValue('border-bottom-width')
                + ' ' + style.getPropertyValue('border-bottom-style')
                + ' ' + style.getPropertyValue('border-bottom-color')
                + ';' + style.getPropertyValue('border-left-width')
                + ' ' + style.getPropertyValue('border-left-style')
                + ' ' + style.getPropertyValue('border-left-color');
    }
    // highlight the element
    elem.style.border = '2px solid red';
    return border;
    

    SCRIPT_UNHIGHLIGHT_ELEMENT

    var elem = arguments[0];
    var borders = arguments[1].split(';');
    elem.style.borderTop = borders[0];
    elem.style.borderRight = borders[1];
    elem.style.borderBottom = borders[2];
    elem.style.borderLeft = borders[3];
    

    Any questions, notes, requests and improvements are welcome!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone please help me figure out a way to achieve the following (see
Can someone please help me out... I've tried all kinds of things (including help
Can someone please help me understand the following: In the previous version of NHibernate
Can someone please help with the following: I am trying to force an asp.net
Can someone please help convert the following into LINQ To SQL query? select *
Can someone please help me with the following code? The methods runs until ss.accept();
Can someone please help me fill in the following function in R: #data is
Can someone please help me convert the following two lines of python to C#.
Can someone please help me write the following, without using dot notation: self.bounds.size.width I
Can someone please help me in writing the exact query, which can give me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.