Selenium WebDrive Java Firefox Headless无法正常运行

时间:2019-07-11 16:39:18

标签: java selenium selenium-webdriver automated-tests

我正在尝试在Macbook上本地运行Fiorefox,但到目前为止还没有运气。我正在使用Selenium WebDrive Java。

我尝试了这两种方法来解决问题,除了完全忽略了headless选项之外,它们都可以正常工作。

package Follo;

public class Class_4 {

    @Test
    public void Firefox1() {

        System.setProperty("webdriver.gecko.driver", "/Users/alessandroborini/Dropbox/Firefox Drivers/geckodriver");

        FirefoxBinary firefoxBinary = new FirefoxBinary();
        firefoxBinary.addCommandLineOptions("--headless");
        FirefoxOptions options = new FirefoxOptions();
        WebDriver driver = new FirefoxDriver(options);
        options.setBinary(firefoxBinary);

        driver.get("https://www.google.com");
        driver.quit();
    }

    @Test
    public void Firefox2() {

        System.setProperty("webdriver.gecko.driver", "/Users/alessandroborini/Dropbox/Firefox Drivers/geckodriver");

        FirefoxOptions options = new FirefoxOptions();
        WebDriver driver = new FirefoxDriver(options);
        driver.manage().window().maximize();
        options.setHeadless(true);
        driver.get("https://www.google.com");
        driver. quit();
    }
}

在测试开始时我有一些警告,但从我看来,它们似乎与我的问题无关。

这是控制台中的警告,但没有错误:

1562862838014   addons.webextension.screenshots@mozilla.org WARN    Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: mozillaAddons
1562862838014   addons.webextension.screenshots@mozilla.org WARN    Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: telemetry
1562862838015   addons.webextension.screenshots@mozilla.org WARN    Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: resource://pdf.js/
1562862838015   addons.webextension.screenshots@mozilla.org WARN    Loading extension 'screenshots@mozilla.org': Reading manifest: Invalid extension permission: about:reader*
1562862841133   Marionette  INFO    Listening on port 57308
1562862841209   Marionette  WARN    TLS certificate errors will be ignored for this session
Jul 11, 2019 5:34:01 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C```

```PASSED: Firefox1
PASSED: Firefox2

===============================================
    Default test
    Tests run: 2, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================```

2 个答案:

答案 0 :(得分:0)

代替调整二进制代码的命令行,直接在选项中直接设置无头标志:

FirefoxBinary firefoxBinary = new FirefoxBinary();
FirefoxOptions options = new FirefoxOptions();
options.setBinary(firefoxBinary);
options.setHeadless(true);  // <-- headless set here
WebDriver driver = new FirefoxDriver(options);

答案 1 :(得分:0)

在您的代码中添加此行,将对您有所帮助

System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,“ firefoxLog”); java.util.logging.Logger.getLogger(“ org.openqa.selenium”)。setLevel(Level.OFF);