单击 Facebook 发布按钮时出现 java.lang.IllegalMonitorStateException

标签 java selenium selenium-webdriver action webdriverwait

该元素已启用并显示。但是,当我尝试单击按钮元素时出现错误。

Error: java.lang.IllegalMonitorStateException

查看我的代码以获取更多详细信息。

Actions actions = new Actions(driver);
actions.moveToElement(element, element.getLocation().x, element.getLocation().y).wait(3000);
element.click();

最佳答案

IllegalMonitorStateException

根据 Java 文档 IllegalMonitorStateException抛出该异常表示一个线程试图在对象的监视器上等待,或者通知在不拥有指定监视器的情况下在对象的监视器上等待的其他线程。

public class IllegalMonitorStateException
extends RuntimeException

继承自类java.lang.Object的相关方法具体如下:

  • Object.notify():唤醒正在该对象监视器上等待的单个线程。
  • Object.notifyAll():唤醒在此对象监视器上等待的所有线程。
  • Object.wait():使当前线程等待,直到另一个线程调用 notify() 方法或 notifyAll()该对象的方法。
  • Object.wait(long):导致当前线程等待,直到另一个线程调用 notify() 方法或 notifyAll() 该对象的方法,或者已经过了指定的时间。
  • Object.wait(long, int):使当前线程等待,直到另一个线程调用 notify() 方法或 notifyAll() 该对象的方法,或者某个其他线程中断了当前线程,或者已经过了一定的实时时间。
<小时/>

当您使用 Selenium 时这些 wait() 方法不能在这里使用,您需要使用 WebDriverWaitExpectedConditions 结合使用您可以使用以下解决方案:

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

关于单击 Facebook 发布按钮时出现 java.lang.IllegalMonitorStateException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57024840/

相关文章:

java - 如何在 JNI 中实例化一个类

java - 相交在 HQL 中不起作用

python - 使用 Tkinter、Threading 和 After 方法

java - eclipse 无法识别 PhantomJSDriverService

java - 使用 Java 8 在 Windows 上调试 spark 进程

java - Jenkins/Buildr 运行 eclipse 编译器为 'javac',无需更改系统 java

python - 使用 python 通过 Selenium WebDriver 计算页面加载的准确时间

javascript - 如何在实习生框架中执行带有循环的代码块

selenium - 使用 GUI 进行自动化网站测试

java - "Element not found in the cache - perhaps the page has changed since it was looked up"显示