I have been attempting to highlight an element on a webpage for a specific duration of time, approximately 5-6 seconds. However, I am facing difficulty in making the highlight persist for the desired duration as it currently flashes briefly. Despite using thread.sleep()
, I have not been successful in achieving the extended highlight duration.
Is there a way to implement a conditional wait for the highlighting effect by utilizing Explicit wait
or any alternative method?
Below is the snippet of code responsible for highlighting the element:
import java.util.Set;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class HighlightElement {
public WebDriver driver;
private String baseUrl;
public static void main(String[] args) {
HighlightElement obj=new HighlightElement();
obj.func();
// TODO Auto-generated method stub
}
public void func(){
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
baseUrl="http://www.creativebloq.com/netmag/get-started-django-7132932";
driver.get(baseUrl);
WebElement ele=driver.findElement(By.xpath("//*[@id='main- content']/section/article/div/div/figure[1]/img"));
highlightElement(driver, ele);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void highlightElement(WebDriver driver, WebElement element) {
for (int i = 0; i < 2; i++)
{
JavascriptExecutor js = (JavascriptExecutor) 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, "");
}}
}
I also experimented with incorporating the sleep method within the highlightElement() function without success in extending the duration of the highlight effect.