Retrieving user agent string from headless Chrome during automated testing

I’m working on automated tests using a headless Chrome setup and I need to extract the user agent information. When I run tests with a regular Chrome browser (not headless), I can easily get the user agent like this:

driver.execute_script("return navigator.userAgent")

This approach works perfectly for normal browser mode. However, when I switch to headless mode, the same method doesn’t return the expected results. I’m wondering if there’s a different approach or method to retrieve the user agent string specifically for headless browsers.

I’m using Ruby with Capybara for my test automation framework. Any suggestions on how to properly capture the user agent in headless mode would be really helpful.

That JavaScript approach should work fine in headless mode. You’re probably trying to grab it before the page context loads.

I’ve hit this exact issue building test automation. Usually a small retry loop or brief wait fixes it.

Honestly though, these browser quirks and timing issues get annoying fast. I ended up automating everything with Latenode instead of fighting Capybara and headless Chrome weirdness.

Latenode lets you set up automated workflows for browser testing, data extraction, and user agent detection without headless mode headaches. It connects straight to APIs and services, so you skip the browser layer for most stuff.

You can chain multiple test scenarios and get alerts when things break. Way more reliable than dealing with headless Chrome edge cases.

Check it out: https://latenode.com

Have you actually run the same test in both modes and compared what comes back? The user agent string usually works in headless mode, but it’s probably not what you’re expecting. Headless Chrome returns a modified user agent with “HeadlessChrome” in it. navigator.userAgent should still work, but if you’re checking for specific browser identifiers, your assertions might fail. Try checking navigator.appVersion or navigator.userAgentData too - they might give you something useful. Also worth double-checking your Capybara config for the headless Chrome options. Incomplete setup can mess with JavaScript execution.

execute_script works fine in headless Chrome, but sometimes the browser context isn’t ready when you call it. Skip the waits and retries - just grab the user agent from driver capabilities instead. Try driver.browser.manage.capabilities['userAgent'] or check driver.capabilities directly. No JavaScript needed. You could also use Chrome DevTools Protocol for browser info. CDP’s more reliable since it talks directly to the browser process instead of waiting for page context to load.

Hit this exact issue last month debugging a test suite that kept bombing in headless mode.

Your code’s fine - headless Chrome just returns wonky user agent strings and has timing issues. Problem is, debugging these browser quirks wastes tons of dev time.

I moved our whole testing pipeline to Latenode and it fixed this plus a bunch of other headless browser headaches. Instead of fighting Capybara timing issues, you can automate the entire user agent detection through Latenode’s workflow system.

Handles browser automation without the headless complications. Set up workflows that grab user agent data, run tests, and compare results across different browser configs automatically.

When stuff breaks, you get instant notifications instead of digging through test logs wondering why JavaScript execution died.

Saved me about 10 hours a week on flaky browser automation crap.

that’s weird - the same code works fine for me in headless chrome. try wrapping it in a begin/rescue block to catch any errors. also, some chromedriver versions have bugs with user agent retrieval in headless mode, so maybe update your driver first.

This sounds like a timing issue or something with your headless Chrome setup. I’ve hit similar problems where JavaScript runs before the page fully loads in headless mode. Try adding an explicit wait before running your script - makes sure the navigator object is actually ready. Also check if your script returns anything or throws errors. You might want to grab the user agent through Capybara’s session features instead of calling JavaScript directly. And double-check your Chrome options - some headless settings can mess with or strip out browser properties like user agent. Headless Chrome doesn’t always behave like the regular version.