Selenium + PhantomJS - missing elements in Page So

2019-09-21 08:18发布

问题:

I've got a Selenium project + Java + Maven that runs normally on Chrome driver. However, I've been trying to make it work with PhantomJS, but I've encountered a problem. The same code that works on Chrome driver doesn't work on PhantomJS. I've debugged a lot and realized that the problem is that the page source that gets loaded is different with PhantomJS. The IDs that I try to locate are not present.

When using System.out.println(driver.getPageSource());, the output for: Chrome driver: length: 19,233 PhantomJS driver: length: 14,965

I've tried to set size as someone suggested in another post (driver.manage().window().setSize(new Dimension(2000, 1500));), but that doesn't help either.

I've tried waiting longer for the page to load, didn't help.

I've also checked the link in driver.get() and it's a full URL with http:// at the beginning.

As we're going to need all the tests to run both on Chrome and on PhantomJS, I'd rather if we didn't need to check the PageSource everytime we write a test and look for something that's present on both.

I'd appreciate any help. :)

回答1:

PhantomJs is having bug these days because of poorly maintenance of it.

You can use chromedriver itself for headless jobs.

You just need to pass one option in chromedriver as below:-

    chromeOptions.addArguments("--headless");

Full code will appear like this :-

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://www.google.co.in/");

Hope it will help you :)