我试着用Selenium打开一页。我也进口了jar。但这行不通。
下面是我的代码: Package 组织举例说明;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Main {
static String url = "link";
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get(url);
System.out.println("Wait a bit for the page to render");
}
}
我得到以下输出:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
[0.037][INFO]: received WebDriver reques: GET /saus
[0.037][INFO]: sending WebDriver response: 200 {
"sessionId": "",
"saus": 0,
"value": {
"build": {
"version": "alpha"
},
"os": {
"arch": "x86_64",
"name": "Windows NT",
"version": "6.2"
}
}
}
[0.461][INFO]: received WebDriver reques: POST /session {
"capabiliies": {
"firsMach": [ {
"browserName": "chrome",
"goog:chromeOpions": {
"args": [ ],
"exensions": [ ]
}
} ]
},
"desiredCapabiliies": {
"browserName": "chrome",
"goog:chromeOpions": {
"args": [ ],
"exensions": [ ]
}
}
}
[0.462][INFO]: Launching chrome: --ignore-cerificae-errors
[0.464][INFO]: sending WebDriver response: 200 {
"sessionId": "",
"saus": 13,
"value": {
"message": "unknown error: chrome failed o sar\n (Driver info: chromedriver=2.0,plaform=Windows NT 6.2 x86_64)"
}
}
[0.546][INFO]: received WebDriver reques: GET /shudown
[0.546][INFO]: sending WebDriver response: 200 {
"sessionId": "",
"saus": 0,
"value": null
}
Started ChromeDriver (v2.0) on port 54751
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:587)
at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:264)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:179)
at org.openqa.selenium.chromium.ChromiumDriver.<init>(ChromiumDriver.java:101)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:81)
at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:49)
at org.example.Main.main(Main.java:15)
Caused by: org.openqa.selenium.WebDriverException: unknown error: chrome failed to start
(Driver info: chromedriver=2.0,platform=Windows NT 6.2 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 329 milliseconds
Build info: version: '4.3.0', revision: 'a4995e2c09*'
System info: host: '*************', ip: '************', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '18'
Driver info: driver.version: ChromeDriver
at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:67)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:483)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:217)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:169)
at org.openqa.selenium.remote.JsonWireProtocolResponse.lambda$errorHandler$0(JsonWireProtocolResponse.java:54)
at org.openqa.selenium.remote.HandshakeResponse.lambda$getResponseFunction$0(HandshakeResponse.java:30)
at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$3(ProtocolHandshake.java:151)
at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:197)
at java.base/java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:1002)
at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:129)
at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:527)
at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:513)
at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:499)
at java.base/java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:150)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.findFirst(ReferencePipeline.java:647)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:153)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:102)
at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:67)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:156)
at org.openqa.selenium.remote.service.DriverCommandExecutor.invokeExecute(DriverCommandExecutor.java:167)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:142)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:569)
... 6 more
Process finished with exit code 1
我懂Java,但我是Selenium的新手。我在Eclipse中尝试过,但它仍然不起作用。
1条答案
按热度按时间k4aesqcs1#
此错误消息...
...意味着ChromeDriver无法启动/产生新的 * 浏览上下文 *,即google-chrome会话。
您的主要问题是您使用的二进制文件版本之间的不兼容:
因此,chromedriver=91.0 和 chrome=96.0.4664.45 之间存在明显的不匹配
解决方案
确保: