0

The element is enabled and is displayed. However, I get an error when trying to click on the button element.

Error: java.lang.IllegalMonitorStateException

Checkout my code for more details.

Actions actions = new Actions(driver);
actions.moveToElement(element, element.getLocation().x, element.getLocation().y).wait(3000);
element.click();
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

1

IllegalMonitorStateException

As per the Java Docs IllegalMonitorStateException is thrown to indicate that a thread has attempted to wait on an object's monitor or to notify other threads waiting on an object's monitor without owning the specified monitor.

public class IllegalMonitorStateException
extends RuntimeException

The relevant methods inherited from class java.lang.Object are as follows:

  • Object.notify(): Wakes up a single thread that is waiting on this object's monitor.
  • Object.notifyAll(): Wakes up all threads that are waiting on this object's monitor.
  • Object.wait(): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object.
  • Object.wait(long): Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
  • Object.wait(long, int): Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.

As you are using Selenium these wait() methods can't be used here and you need to use WebDriverWait in conjunction with ExpectedConditions and you can use the following solution:

WebElement element = driver.findElement(By.cssSelector("css_of_the_element"))
new Actions(driver).moveToElement(new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(element))).click().build().perform();

Here you can find a relevant discussion on Difference between driver.manage.wait(long timeout) and Explicit wait

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

You can only wait on an object if you've acquired the lock for it using synchronized.

synchronized (driver)
{
    driver.wait();
}

Try this:

 synchronized(actions){
 actions.moveToElement(element, element.getLocation().x, element.getLocation().y).wait(3000);
 } 
GOVIND DIXIT
  • 1,748
  • 10
  • 27