selenium空白浏览器

4nkexdtk  于 2021-08-25  发布在  Java
关注(0)|答案(1)|浏览(420)

我有一个使用selenium browser启动测试的项目,当我尝试使用firefox时,我收到错误消息:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.openqa.selenium.WebDriver]: Factory method 'getWebDriver' threw exception; nested exception is java.lang.NoClassDefFoundError: Could not initialize class fr.company.gui.test.webdrivers.FirefoxWebDriver
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:651)
    ... 45 more
Caused by: java.lang.NoClassDefFoundError: Could not initialize class fr.company.gui.test.webdrivers.FirefoxWebDriver
    at fr.company.gui.test.contexts.CucumberContext.getWebDriver(CucumberContext.java:96)
    at fr.company.gui.test.contexts.CucumberContext$$EnhancerBySpringCGLIB$$3688dac0.CGLIB$getWebDriver$0(<generated>)
    at fr.company.gui.test.contexts.CucumberContext$$EnhancerBySpringCGLIB$$3688dac0$$FastClassBySpringCGLIB$$b0388c82.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
    at fr.company.gui.test.contexts.CucumberContext$$EnhancerBySpringCGLIB$$3688dac0.getWebDriver(<generated>)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
    ... 46 more

班级 CucumberContext ,尝试示例化 FirefoxWebDriver 对象,该行如下所示:
驱动程序=新的firefoxwebdriver();
班级 FirefoxWebDriver 延伸 org.openqa.selenium.support.events.EventFiringWebDriver .

错误是由以下原因引起的:

Caused by: org.openqa.selenium.TimeoutException: Socket timeout reading Marionette handshake data: Une tentative de connexion a échoué car le parti connecté n’a pas répondu convenablement au-delà d’une certaine durée ou une connexion établie a échoué car l’hôte de connexion n’a pas répondu. (os error 10060)
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'XXXXX', ip: 'XXX.XXX.XXX.XXX', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_73'
Driver info: driver.version: FirefoxWebDriver
remote stacktrace: 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    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.util.stream.ReferencePipeline$3$1.accept(Unknown Source)
    at java.util.Spliterators$ArraySpliterator.tryAdvance(Unknown Source)
    at java.util.stream.ReferencePipeline.forEachWithCancel(Unknown Source)
    at java.util.stream.AbstractPipeline.copyIntoWithCancel(Unknown Source)
    at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
    at java.util.stream.FindOps$FindOp.evaluateSequential(Unknown Source)
    at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
    at java.util.stream.ReferencePipeline.findFirst(Unknown Source)
    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.firefox.FirefoxDriver.<init>(FirefoxDriver.java:147)
    at fr.company.gui.test.webdrivers.FirefoxWebDriver.<clinit>(FirefoxWebDriver.java:38)
    ... 57 more

这个 FirefoxWebDriver 无法示例化 FirefoxDriver ,代码如下:

public class FirefoxWebDriver extends EventFiringWebDriver {
    ...
    static {
        System.setProperty("java.awt.headless", "false");
        FirefoxOptions capabilities = new FirefoxOptions();
        capabilities.setCapability("marionette", true);
        capabilities.addPreference("network.proxy.type", 1);
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("network.proxy.http_port", 8080);

        System.out.println("############## THIS MESSAGE WILL BE PRINTED ##############");
        webdriver = new FirefoxDriver(capabilities);
        System.out.println("############## NOT THIS ONE ##############");
        ...
    }
    ...
}

壁虎河是第28条河 geckodriver-v028.exe ,我使用的firefox是 Firefox 85.0.1 (32 bits) ,pom.xml中使用的selenium版本为 <selenium.version>3.141.59</selenium.version> .

我看到:

启动项目后,我可以看到firefox浏览器,但它是一个空白页面:
空白firefox浏览器

编辑

这个 CucumberContext 类有一个方法 WebDriver getWebDriver(XService xservice, String webbrowser) . 在这种方法中,您有:

public WebDriver getWebDriver(XService xservice, String webbrowser) {
        ...
        final File driverFile = xservice.getFileDriver(webbrowser);
        switch (webbrowser) {
            case "firefox":
                System.setProperty("webdriver.gecko.driver", driverFile.getPath());
                driver = new FirefoxWebDriver();
                break;
        ...
    }

XService 方法 getFileDriver 返回驱动程序文件的方法。

return createOrLoadFileDriver("driver_firefox.exe", "/geckodriver-v028.exe");

正确找到驱动程序文件,然后用另一种方法复制它 createOrLoadFileDriver 有名字 driver_firefox.exe . 此方法工作正常。

xlpyo6sf

xlpyo6sf1#

此处缺少引用变量:

webdriver = new FirefoxDriver(capabilities);

代码:

static {
        System.setProperty("java.awt.headless", "false");
        FirefoxOptions capabilities = new FirefoxOptions();
        capabilities.setCapability("marionette", true);
        capabilities.addPreference("network.proxy.type", 1);
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("network.proxy.http_port", 8080);

        System.out.println("############## THIS MESSAGE WILL BE PRINTED ##############");
        driver = new FirefoxDriver(capabilities);
        System.out.println("############## NOT THIS ONE ##############");

    }

您可以在类中声明如下引用变量:

protected static WebDriver driver;

相关问题