Htmlunit driver has trouble with Javascript
You don't even need to use PhantomJs. As PhantomJs is not so much maintain these days. You can use chromedriver in headless mode.
you just need to add options as headless as below :-
chromeOptions.addArguments("--headless");
Please find complete code below:
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");
While if still you want to use phantomjs. then first download phantomjs binary from below location :-
http://phantomjs.org/download.html
Now use below code :-
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/");
Hope it will help you :)