Highlight elements in WebDriver during runtime

后端 未结 10 1190
离开以前
离开以前 2020-12-02 15:54

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

10条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 16:30

    Here the code for higlighting the element in selenium: First create a class:

    package com.demo.misc.function;
    
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    
    public class Miscellaneous 
    {
        public static void highLight(WebElement element, WebDriver driver)
        {
            for (int i = 0; i <2; i++) 
            {
                try {
                    JavascriptExecutor js = (JavascriptExecutor) driver;
                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: black; border: 4px solid red;");
                    Thread.sleep(500);
                    js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "");
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
    
            }
    
        }
    
    }
    

    And then you can call this by :

    driver.get(baseUrl);
                Thread.sleep(2000);
                WebElement lnkREGISTER = driver.findElement(By.linkText("REGISTER"));
                Miscellaneous.highLight(lnkREGISTER, driver);
                lnkREGISTER.click();
    

提交回复
热议问题