Selenium webdriver is unable to locate element in chrome headless mode

Viewed 4358

I wrote the following script to practice Selenium browser automation. It accesses Steam's website and changes your Steam username. It works perfectly if run without Chrome's headless mode but fails to locate the very first element if started with options.addArguments("headless"). The code:

@Test
public void steamPowered() throws IOException {

    System.setProperty("webdriver.chrome.driver", "C:\\Driver\\chromedriver.exe");

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--user-data-dir=C://Users/Evan/Downloads/Profile8Aug17");
    options.addArguments("headless");
    options.addArguments("window-size=1200x600");

    ChromeDriver driver = new ChromeDriver(options);

    driver.navigate().to("https://store.steampowered.com/");

    WebElement element = (new WebDriverWait(driver, 10))
            .until(ExpectedConditions.presenceOfElementLocated(By.id("account_pulldown")));
    element.click();

    element = (new WebDriverWait(driver, 10))
            .until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[@id=\"account_dropdown\"]/div/a[4]")));
    element.click();

    element = (new WebDriverWait(driver, 10))
            .until(ExpectedConditions.presenceOfElementLocated(By.xpath
            ("/html/body/div[1]/div[7]/div[2]/div[1]/div[1]/div/div/div/div[3]/div[2]/a/span")));
    element.click();

    element = (new WebDriverWait(driver, 10))
            .until(ExpectedConditions.presenceOfElementLocated(By.id("personaName")));
    element.clear();
    element.sendKeys(scramble(USERNAME));

    driver.findElement(By.xpath("//span[text()='Save Changes']")).click();

    driver.quit();
}

}

The intellij printout when it crashes:

org.openqa.selenium.NoSuchElementException: no such element: Unable to locate 
element: {"method":"id","selector":"account_pulldown"}
  (Session info: headless chrome=60.0.3112.90)
  (Driver info: chromedriver=2.31.488763 
(092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 6.1.7601 SP1 
x86_64) (WARNING: The server did not provide any stacktrace information)

This is confusing because the following code works perfectly (copy pasted from https://medium.com/@eliasnogueira/running-selenium-tests-with-chrome-headless-5edd624efb92)

@Test
public void testExecution() throws IOException {
    System.setProperty("webdriver.chrome.driver", "C:\\Driver\\chromedriver.exe");

    // Add options to Google Chrome. The window-size is important for responsive sites
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--user-data-dir=C://Users/Evan/Downloads/Profile8Aug17");
    options.addArguments("headless");
    options.addArguments("window-size=1200x600");

    WebDriver driver = new ChromeDriver(options);
    driver.get("http://seleniumhq.org");

    // a guarantee that the test was really executed
    assertTrue(driver.findElement(By.id("q")).isDisplayed());

    driver.quit();
}

So what am I missing here? I fail to see any serious difference between the two. What's stopping my script from locating page elements in headless mode?

3 Answers

try changing screen size to "1920x1080" sometimes you do find trouble when screen size is inappropriate.

Can you please try with the belwo settings:

options.addArguments("window-size=1400,800");
options.addArguments("headless");

In order to use headless chrome using ChromeOptions in java you are supposed to use --headless, you may just replace:

options.addArguments("headless");

with:

options.addArguments("--headless");
Related