你甚至不需要使用PhantomJs。由於PhantomJs現在還沒有太多維護。您可以在無頭模式下使用chromedriver。
你只需要添加的選項,如下無頭: -
chromeOptions.addArguments("--headless");
請在下面找到完整代碼:
System.setProperty("webdriver.chrome.driver","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://google.com");
如果仍然要使用phantomjs雖然。然後先下載phantomjs二進制從下面的位置: -
http://phantomjs.org/download.html
現在使用下面的代碼: -
System.setProperty("phantomjs.binary.path","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\phantomjs\\phantomjs.exe");
DesiredCapabilities capabilities = null;
ArrayList<String> cliArgsCap = new ArrayList<String>();
capabilities = DesiredCapabilities.phantomjs();
cliArgsCap.add("--web-security=false");
cliArgsCap.add("--ssl-protocol=any");
cliArgsCap.add("--ignore-ssl-errors=true");
capabilities.setCapability("takesScreenshot", true);
capabilities.setJavascriptEnabled(true);
capabilities.setCapability(
PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
capabilities.setCapability(
PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS,new String[] { "--logLevel=2" });
driver = new PhantomJSDriver(capabilities);
driver.get("https://www.google.co.in/");
希望它會幫助你:)
爲什麼要使用HtmlUnitDriver? –
@TarunLalwani因爲它使用比firefox更少的資源 – Mit94
那麼最好使用PhantomJS。 'HtmlUnitDriver'不是最適合測試的驅動程序。 –