如何使用java项目修复selenium的Edge和chrome驱动错误?

问题描述 投票:0回答:2

我下载了边缘驱动程序发布版本88

将 selenium 独立服务器 jar 文件导入到我的 java 项目中。

尝试执行以下代码。

//TestEdge.java
package com.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;

public class TestChrome {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.edge.driver", "C:\\libs\\msedgedriver.exe");
        WebDriver driver = new EdgeDriver();
        driver.get("http://www.seleniumhq.org/download/");
        driver.quit();
        
    }

}

但是低于错误。

Starting MSEdgeDriver 88.0.688.0 (d760c7e40eea1524171f33a22f8c78145ceb0afe) on port 41783
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping MSEdgeDriver safe.
MSEdgeDriver was started successfully.
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: cannot find MSEdge binary
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'DESKTOP-189RBQR', ip: '192.168.0.7', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.9'
Driver info: driver.version: EdgeDriver
remote stacktrace: Backtrace:
    Ordinal0 [0x00007FF7BC5832D2+4272850]
    Ordinal0 [0x00007FF7BC456859+3041369]
    Ordinal0 [0x00007FF7BC2ACE37+1297975]
    Ordinal0 [0x00007FF7BC1EF2BC+520892]
    Ordinal0 [0x00007FF7BC1EC4B0+509104]
    Ordinal0 [0x00007FF7BC222F42+732994]
    Ordinal0 [0x00007FF7BC21D98F+711055]
    Ordinal0 [0x00007FF7BC1F8CF5+560373]
    Ordinal0 [0x00007FF7BC1F9ADB+563931]
    Ordinal0 [0x00007FF7BC476808+3172360]
    GetHandleVerifier [0x00007FF7BC6D5D13+1260179]
    GetHandleVerifier [0x00007FF7BC6E78B6+1332790]
    GetHandleVerifier [0x00007FF7BC6D66D9+1262681]
    Ordinal0 [0x00007FF7BC469B45+3119941]
    Ordinal0 [0x00007FF7BC478BFB+3181563]
    Ordinal0 [0x00007FF7BC48DE3A+3268154]
    BaseThreadInitThunk [0x00007FFA43BD7C24+20]
    RtlUserThreadStart [0x00007FFA455CCEA1+33]

    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$errorHandler$0(W3CHandshakeResponse.java:62)
    at org.openqa.selenium.remote.HandshakeResponse.lambda$getResponseFunction$0(HandshakeResponse.java:30)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:126)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
    at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:127)
    at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:502)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:488)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    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:543)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:128)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:74)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
    at org.openqa.selenium.edge.EdgeDriver.<init>(EdgeDriver.java:141)
    at org.openqa.selenium.edge.EdgeDriver.<init>(EdgeDriver.java:96)
    at com.test.TestChrome.main(TestChrome.java:12)

关于如何解决这个问题有什么建议吗?

我正在尝试使用openjdk 11最新版本。

我也尝试使用 chrome 驱动程序,但出现以下错误。

package com.test;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.edge.EdgeDriver;

public class TestChrome {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "C:\\libs\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.seleniumhq.org/download/");
        driver.quit();
        
    }

}

错误结果:

Starting ChromeDriver 87.0.4280.20 (c99e81631faa0b2a448e658c0dbd8311fb04ddbd-refs/branch-heads/4280@{#355}) on port 17563
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: session not created: This version of ChromeDriver only supports Chrome version 87
Current browser version is 86.0.4240.183 with binary path C:\Program Files\Google\Chrome\Application\chrome.exe
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'DESKTOP-189RBQR', ip: '192.168.0.7', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.9'
Driver info: driver.version: ChromeDriver
remote stacktrace: Backtrace:
    Ordinal0 [0x012EECE3+3337443]
    Ordinal0 [0x011CF041+2158657]
    Ordinal0 [0x01056FA8+618408]
    Ordinal0 [0x00FD1D52+73042]
    Ordinal0 [0x00FCE0D8+57560]
    Ordinal0 [0x00FCC38A+50058]
    Ordinal0 [0x00FF5E8E+220814]
    Ordinal0 [0x00FF5BFC+220156]
    Ordinal0 [0x00FF181B+202779]
    Ordinal0 [0x00FD3D74+81268]
    Ordinal0 [0x00FD4D6E+85358]
    Ordinal0 [0x00FD4CF9+85241]
    Ordinal0 [0x011E6F2C+2256684]
    GetHandleVerifier [0x01472FAC+1478332]
    GetHandleVerifier [0x01472AFD+1477133]
    GetHandleVerifier [0x0147AAD8+1509864]
    GetHandleVerifier [0x0147369E+1480110]
    Ordinal0 [0x011DD6BD+2217661]
    Ordinal0 [0x011E89AB+2263467]
    Ordinal0 [0x011E8AEF+2263791]
    Ordinal0 [0x011FD363+2347875]
    BaseThreadInitThunk [0x77276359+25]
    RtlGetAppContainerNamedObjectPath [0x77677C24+228]
    RtlGetAppContainerNamedObjectPath [0x77677BF4+180]

    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
    at org.openqa.selenium.remote.W3CHandshakeResponse.lambda$errorHandler$0(W3CHandshakeResponse.java:62)
    at org.openqa.selenium.remote.HandshakeResponse.lambda$getResponseFunction$0(HandshakeResponse.java:30)
    at org.openqa.selenium.remote.ProtocolHandshake.lambda$createSession$0(ProtocolHandshake.java:126)
    at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
    at java.base/java.util.Spliterators$ArraySpliterator.tryAdvance(Spliterators.java:958)
    at java.base/java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:127)
    at java.base/java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:502)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:488)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    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:543)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:128)
    at org.openqa.selenium.remote.ProtocolHandshake.createSession(ProtocolHandshake.java:74)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:136)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.startSession(RemoteWebDriver.java:213)
    at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:131)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:181)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:168)
    at org.openqa.selenium.chrome.ChromeDriver.<init>(ChromeDriver.java:123)
    at com.test.TestChrome.main(TestChrome.java:12)
java selenium selenium-webdriver selenium-chromedriver microsoft-edge
2个回答
0
投票

从错误消息来看,我认为这是因为您使用了与浏览器版本不同的网络驱动程序版本。您需要使用匹配的 webdriver 版本。

例如,如果您使用 Chrome 86.0.4240.183,则需要使用 ChromeDriver 86.0.4240.22。您也可以在此页面查看并下载。 Edge 也是如此。请检查您的 Edge 浏览器版本并在此页面下载相同版本的 Edge WebDriver。


-1
投票
System.setProperty("webdriver.edge.driver", "C:/Tools/webdrivers/msedgedriver.exe");

DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setPlatform(Platform.WINDOWS);
desiredCapabilities.setBrowserName(BrowserType.EDGE);
desiredCapabilities.setBrowserName("MicrosoftEdge");

Map<String, Object> edgePrefs = new HashMap<String, Object>();
edgePrefs.put("profile.default_content_settings.popups", 0);
edgePrefs.put("download.prompt_for_download", false);


EdgeOptions edgeOptions = new EdgeOptions();
edgeOptions.setCapability("ms:inPrivate", true);
edgeOptions.setCapability("prefs", edgePrefs);
edgeOptions.setCapability("useAutomationExtension", false);


edgeOptions.merge(desiredCapabilities);
edgeOptions.setPageLoadStrategy("eager");
edgeOptions.setCapability("ms:inPrivate", true);
edgeOptions.setCapability("useAutomationExtension", false);
edgeOptions.setCapability(CapabilityType.HAS_NATIVE_EVENTS, true);
edgeOptions.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);
edgeOptions.setCapability(CapabilityType.HAS_NATIVE_EVENTS, true);
edgeOptions.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, true);

driver = new EdgeDriver(edgeOptions)
© www.soinside.com 2019 - 2024. All rights reserved.