Selenium test fails with element visibility error in headless mode on CI server but works fine locally with regular browser

I’m having trouble with my automated tests when they run on our Jenkins build server. The tests work perfectly when I run them on my own computer using a regular Chrome browser, but they fail when running in headless mode on the Jenkins Windows agent.

The specific error I keep getting is org.openqa.selenium.ElementNotVisibleException saying the element cannot be seen or interacted with.

I’ve already checked a few things:

  • The element definitely exists on the webpage
  • I added some explicit waits and even tried Thread.sleep() to give the page more time to load
  • The same test code runs without any issues locally

The main difference is that locally I use Chrome with a visible window, while on Jenkins it runs in headless mode. Has anyone encountered this issue before? What could be causing elements to behave differently between headless and regular browser modes?

I’ve hit this headless browser issue tons of times. It’s usually a viewport problem - headless Chrome defaults to a tiny window size that breaks your layout or triggers mobile breakpoints that hide elements. Set --window-size=1920,1080 in your Chrome config and that’ll fix most cases. Also watch out for CSS animations - elements might be there but still moving when Selenium tries to click them. And double-check your Jenkins box has the right fonts installed, because missing resources mess up layouts too.

also, check timing - headless mode loads at diff speeds than regular chrome, so ur waits might be off. use webdriverwait with expected conditions instead of thread.sleep. elements can render differently too, since gpu acceleration is diff in headless. try adding --disable-gpu to fix rendering issues.

CSS media queries hit me the same way - some sites have specific styles that hide elements when they detect headless browsers. I’d check the computed styles for that element in both modes to spot any differences. Also ran into JS frameworks that delay rendering based on user agent detection, basically treating headless browsers like bots. Try adding the --user-agent flag with a regular browser string and see if that fixes the visibility issue. ElementNotVisibleException also pops up when elements exist in the DOM but have zero dimensions because external stylesheets didn’t load in headless mode.