I am trying to use headless chrome for our selenium tests and have made the below changes:
DesiredCapabilities desiredCapabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless");
options.addArguments("--disable-gpu");
options.addArguments("window-size=1800x1080");
desiredCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
My test logs into an internal page and then waits for the element to be visible:
selenium.waitForElementVisible("xpath=//tr/td/div[@class[contains(., 'x-grid-cell-inner')] and text()='Global Test Merchant 14']");
This all works well when i do not have the headless option but i get :
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//tr/td/div[@class[contains(., 'x-grid-cell-inner')] and text()='Global Test Merchant 14']"}
when i run the test with --headless.
Chrome Version: 62.0.3202.89 chromeDriver: 2.33.506120 Selenium version: 2.53.0 Windows 7
I had the same issue, my mistake was because i was making driver.get("localhost:...") instead of driver.get("http://localhost:...")
As you are seeing NoSuchElementException you can consider using the xpath
along with a waiter for the element to be visible as follows :
//tr/td/div[@class='x-grid-cell-inner' and contains(., 'Global Test Merchant')]
I had a similar issue when I ran headless as well, my project while running headless continue to trigger the NoSuchElementException and the default-browser-check was getting in the way, try adding these arguments. Just a thought
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--test-type");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--no-first-run");
chromeOptions.addArguments("--no-default-browser-check");
chromeOptions.addArguments("--ignore-certificate-errors");
chromeOptions.addArguments("--start-maximized");
来源:https://stackoverflow.com/questions/49520558/nosuchelementexception-with-headless-chrome-and-selenium