java.lang.IllegalMonitorStateException (java selenium)

huangapple go评论77阅读模式
英文:

java.lang.IllegalMonitorStateException (java selenium)

问题

package main_files;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class main_downloader {
    public static void main(String[] args) throws Exception {
        System.setProperty("webdriver.gecko.driver", "path\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.wait(1);
        driver.get("https://www.google.com/");
        driver.wait(5);
        driver.quit();
    }
}

因为某种原因,当我尝试运行这段代码时,它给了我以下错误:

1597445198205	geckodriver	INFO	Listening on 127.0.0.1:7834
1597445198836	mozrunner::runner	INFO	Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "--marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\ahmed\\AppData\\Local\\Temp\\rust_mozprofileinTqzS"
JavaScript error: resource://gre/modules/XULStore.jsm, line 66: Error: Can't find profile directory.
console.error: SearchCache: "_readCacheFile: Error reading cache file:" (new Error("", "(unknown module)"))
1597445201342	Marionette	INFO	Listening on port 22663
1597445201445	Marionette	WARN	TLS certificate errors will be ignored for this session
Aug 15, 2020 1:46:41 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" java.lang.IllegalMonitorStateException
    at java.lang.Object.wait(Native Method)
    at main_files.main_downloader.main(main_downloader.java:10)

我尝试了一些解决方案,但都没有成功!有人可以帮忙吗?谢谢。

英文:
package main_files;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class main_downloader {
	public static void main(String[] args) throws Exception {
		System.setProperty("webdriver.gecko.driver", "path\\geckodriver.exe");
		WebDriver driver = new FirefoxDriver();
		driver.wait(1);
		driver.get("https://www.google.com/");
		driver.wait(5);
		driver.quit();
	}
}

for some reason when I try to run this code it gives me this Error

1597445198205	geckodriver	INFO	Listening on 127.0.0.1:7834
1597445198836	mozrunner::runner	INFO	Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "--marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\ahmed\\AppData\\Local\\Temp\\rust_mozprofileinTqzS"
JavaScript error: resource://gre/modules/XULStore.jsm, line 66: Error: Can't find profile directory.
console.error: SearchCache: "_readCacheFile: Error reading cache file:" (new Error("", "(unknown module)"))
1597445201342	Marionette	INFO	Listening on port 22663
1597445201445	Marionette	WARN	TLS certificate errors will be ignored for this session
Aug 15, 2020 1:46:41 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" java.lang.IllegalMonitorStateException
	at java.lang.Object.wait(Native Method)
	at main_files.main_downloader.main(main_downloader.java:10)

i've tried some solutions but nothing worked !, could anyone help please !? Thx.

答案1

得分: 1

wait()函数调用必须从同步块中调用,以避免此异常。
如果需要的话,您可以使用Thread.sleep()来使主线程休眠,而不是wait()函数调用。

英文:

wait() function call must be called from Synchronized block to avoid this exception.
Do you want to sleep the main thread , if yes please use Thread.sleep() instead of wait() function call.

huangapple
  • 本文由 发表于 2020年8月15日 20:34:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63426093.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定