Highlight elements in WebDriver during runtime

后端 未结 10 1205
离开以前
离开以前 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:32

    In webdriver
    Create a class for highligh element HighlightElement

    HighlightElement.java

    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebElement;
    
    import com.project.setup.WebDriverManager;
    
    public class HighlightElement {
    
        public static void highlightElement(WebElement element) {
            for (int i = 0; i <2; i++) {
                JavascriptExecutor js = (JavascriptExecutor) WebDriverManager.driver;
                js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "color: yellow; border: 2px solid yellow;");
                js.executeScript("arguments[0].setAttribute('style', arguments[1]);", element, "");
                }
            }
    }
    

    You can use

    HighlightElement.highlightElement(driver.findElement(By.xpath("blaah blaah"));)

    to highlight the WebElement with xpath "blaah blaah" in your test.

提交回复
热议问题