java - 从 IDE 运行时 Geckodriver 工作正常,但从可执行 jar 运行时抛出异常

标签 java selenium geckodriver selenium-firefoxdriver

我使用selenium通过firefox下载文件。当我从 IDE (eclipse) 运行时一切正常,但是当我提取可执行 jar 时,出现以下异常:

    Exception in thread "main" org.openqa.selenium.WebDriverException: Build info: version: 'unknown', revision: 'f148142cf8', time: '2019-07-01T21:24:57'
System info: host: '28LJQV2', ip: '192.168.5.40', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_221'
Driver info: driver.version: FirefoxDriver
        at java.util.Optional.orElseThrow(Unknown Source)
        at org.openqa.selenium.firefox.FirefoxDriver.toExecutor(FirefoxDriver.java:179)
        at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:154)

private void initSelenium() {
        System.setProperty("webdriver.gecko.driver",         Config.getInstance().getFIREFOX_DRIVER_PATH());
    }

private WebDriver createFirefoxDriverWithDownloadFolder(String downloadFolder) {
        FirefoxProfile profile = new FirefoxProfile();
        FirefoxOptions options = new FirefoxOptions();
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", downloadFolder);
        profile.setPreference("browser.download.manager.showWhenStarting", false);
        profile.setPreference("browser.helperApps.neverAsk.openFile", "application/x-msdownload");
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk", "application/x-msdownload, application/zip");

        options.setProfile(profile);

        WebDriver driver = new FirefoxDriver(options);
        return driver;
    }

在属性文件中: FIREFOX_DRIVER_PATH = C:\geckodriver.exe

最佳答案

通过使用“将所需的库打包到生成的 jar 中”而不是“将所需的库提取到生成的 jar 中”提取可运行的 jar 来解决

关于java - 从 IDE 运行时 Geckodriver 工作正常,但从可执行 jar 运行时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57706965/

相关文章:

python - Selenium 找不到元素

java - 如何通过 FirefoxProfile 启动 Mozilla 浏览 session ?

selenium - NS_ERROR_SOCKET_ADDRESS_IN_USE

java - Swing : storing the selected values from List into an array.

java - 如何使用docx4j添加PPT注释

java - 在 Java 中检索 Json 数据

node.js - WebdriverIO 屏幕截图在等待执行条件后失败

java - 在使用 : ((JavascriptExecutor)seleniumdriver). executeScript ("return arguments[0].attributes);", webElement 之后,超出了最大调用堆栈大小;

java - 用于许多测试的 geckodriver - Java

java - 谷歌日历(共享)