Reputation: 1857
I have written a selenium code to select a button on browser and to click it but I want selenium to wait for few seconds when it moves to that element.How can I do this? following is my tried code , but it did't work for me.
My code :
Actions actionobj = new Actions(fd1);
actionobj.moveToElement(heatmap);
actionobj.perform();
fd1.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
Actions action2 = new Actions(fd1);
action2.click(heatmap);
action2.perform();
My code works fine but it does't stop for 10 seconds when mouse is moved to button.I have also tried Thread.sleep() but that also did't work.
Upvotes: 1
Views: 6118
Reputation: 1485
If you want to simply wait for few seconds, (just to enable users to understand what is happening) then it can be easily achieved by halting the java thread.
There are so many ways of adding a forced wait.
This is by using simple java (nothing to do with Selenium)
Thread.sleep(<<timeInMilliSeconds>>);
Hope this works for you.
Upvotes: -1
Reputation: 4424
Try this code for highlighting and clicking on the element rather than pausing on it which is rather not a feasible solution for your problem:
//highlighting the element on which action will be performed
public static void highlightElement(WebDriver driver, WebElement element) {
try
{
for (int i = 0; i < 3; i++)
{
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', arguments[1]);",element, "color: red; border: 2px solid red;");
}
}
catch(Throwable t)
{
System.err.println("Error came : " +t.getMessage());
}
}
Note: Above code will highlight the element,i.e., will surround with "red" color block . You have to pass the "driver" and "webelement" as parameters.
You can call this method from your main class directly. And, as per your above code, you can pass the elements like this:
highlightElement(driver,heatmap);
Upvotes: 1
Reputation: 3649
You can also try out this way,
Actions actionobj = new Actions(fd1);
actionobj.moveToElement(heatmap).build().perform();
Thread.sleep(10000);
actionobj.click(heatmap).build().perform();
Upvotes: 2
Reputation: 11093
It seems the Java Selenium API has a method pause(long)
.
According to the documentation it takes a long
which represents the amount of milliseconds to pause for.
Actions actionobj = new Actions(fd1);
actionobj.moveToElement(heatmap);
actionobj.pause(10000); //wait 10 seconds
actionobj.click(heatmap);
actionobj.perform();
imlicitlyWait()
does not pause your code. It's a method to let Selenium always wait for a couple of seconds if a WebElement
is not instantly found.
Note that pause()
is deprecated. It's bad practice to manually pause your code. You should question yourself why you deem it necessary to pause your code. If you want to simulate a human who waits 10 seconds, it perfectly fine, if you want some other elements or javascript to finish loading, then you should consider using different methods.
Edit: and your code doesnt pause halfway (even if you'd use Thread.sleep()
) because on Action.perform()
the whole sequence is carried out, for you build the Actions object first, and then, on perform, you perform the whole sequence of actions put together.
Upvotes: 5
Reputation: 1713
This is because the perform() call is where the action gets performed. Here you will need to create two action first for moveToElement() and perform the action. then create the click action and perform the action.
Hope this helps. If this is not what you want, or you meant something different, please leave a comment.
Upvotes: 1