通过webdrivermanager使用Selenium运行Opera浏览器的问题

时间:2018-12-04 15:32:54

标签: java selenium opera operadriver

使用硒的Opera浏览器出现问题。要下载浏览器二进制文件,我正在使用webdrivermanager插件(https://github.com/bonigarcia/webdrivermanager

这是实现不同类型浏览器的枚举:

public enum DriverType implements DriverSetup {

    CHROME {
        public WebDriver getWebDriverObject() {
            WebDriverManager.chromedriver().setup();
            return new ChromeDriver();
        }
    },
    FIREFOX {
        public WebDriver getWebDriverObject() {
            WebDriverManager.firefoxdriver().setup();
            return new FirefoxDriver();
        }
    },
    OPERA {
        public WebDriver getWebDriverObject() {
            WebDriverManager.operadriver().setup();
            return new OperaDriver();
        }
    }
}

有一个getWebDriverObject方法的调用:

public class DriverBase {

    public WebDriver driver;

    public WebDriver initializeBrowser() {
        DriverType driverType = DriverType.CHROME;

        try {
            String browser = System.getProperty("browser").toUpperCase();
            driver = DriverType.valueOf(browser).getWebDriverObject();
        } catch (Exception e) {
            System.out.println("No driver specified/Wrong driver specified, default tests running on Chrome");
            driver = driverType.getWebDriverObject();
        }
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        return driver;
    }
}

在运行测试之前,我将系统属性浏览器设置为“ opera”。这是输出。默认情况下,测试在chrome中运行,而Opera浏览器没有打开,因此无法正常工作。我该如何解决?

    ProjectName > Login STANDARD_ERROR
    1160 [Test worker] WARN io.github.bonigarcia.wdm.WebDriverManager - The driver version for Opera No Instance(s) Available. is unknown .
.. trying with latest
    1163 [Test worker] INFO io.github.bonigarcia.wdm.WebDriverManager - Reading https://api.github.com/repos/operasoftware/operachromiumdri
ver/releases to seek operadriver
    1465 [Test worker] INFO io.github.bonigarcia.wdm.WebDriverManager - Latest version of operadriver is 2.41
    1548 [Test worker] INFO io.github.bonigarcia.wdm.Downloader - Using binary driver previously downloaded
    1550 [Test worker] INFO io.github.bonigarcia.wdm.WebDriverManager - Exporting webdriver.opera.driver as C:\Users\me\.m2\re
pository\webdriver\operadriver\win64\2.41\operadriver.exe
    Starting OperaDriver 2.41 on port 37865
    Only local connections are allowed.

Socrates > Login STANDARD_OUT
    No driver specified/Wrong driver specified, default tests running on Chrome

1 个答案:

答案 0 :(得分:0)

此错误消息...

INFO io.github.bonigarcia.wdm.WebDriverManager - Exporting webdriver.opera.driver as C:\Users\me\.m2\repository\webdriver\operadriver\win64\2.41\operadriver.exe
    Starting OperaDriver 2.41 on port 37865
    Only local connections are allowed.

Socrates > Login STANDARD_OUT
    No driver specified/Wrong driver specified, default tests running on Chrome

......表示尽管您的程序按照system property的配置访问了 OperaDriver ,但由于驱动程序规范中的不匹配 Chrome浏览器 被调用。

您的主要问题是,尽管您仍将system property方法中的initializeBrowser() 浏览器设置为 opera

DriverType driverType = DriverType.CHROME;

解决方案

按如下所示更改initializeBrowser()方法:

public WebDriver initializeBrowser() {
    DriverType driverType = DriverType.OPERA;

    try {
        String browser = System.getProperty("browser").toUpperCase();
        driver = DriverType.valueOf(browser).getWebDriverObject();
    } catch (Exception e) {
        System.out.println("No driver specified/Wrong driver specified, default tests running on Chrome");
        driver = driverType.getWebDriverObject();
    }

更新

现在您看到的错误为:

org.openqa.selenium.WebDriverException: unknown error: cannot find Opera binary

要解决此问题,您可以按照讨论selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Opera binary with OperaDriver Selenium and Python