i try to run the below code in headless-mode. In non headless-mode everything works fine.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
options = Options()
# options.add_argument('--headless=new')
options.add_argument("--window-size=1920x1080")
options.add_argument("start-maximized")
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('--log-level=3')
options.add_experimental_option("prefs", {"profile.default_content_setting_values.notifications": 1})
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('excludeSwitches', ['enable-logging'])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
srv=Service()
driver = webdriver.Chrome (service=srv, options=options)
waitWD = WebDriverWait (driver, 5)
actions = ActionChains(driver)
driver.get ("https://www.bing.com/maps")
waitWD.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="bnp_btn_accept"]//a'))).click()
waitWD.until(EC.presence_of_element_located((By.XPATH,'//input[@id="maps_sb"]'))).send_keys(f"hairdresser")
actions = actions.send_keys(Keys.ENTER)
actions.perform()
input("Press!")
But when running the code in the headless mode i get this error:
(selenium) C:\DEV\Fiverr2025\TEMPLATES>python test.py
DevTools listening on ws://127.0.0.1:55519/devtools/browser/e278bbb3-064d-4f9c-a76d-aa92833450cd
[39712:27412:0531/224848.940:ERROR:gles2_cmd_decoder_passthrough.cc(1081)] [GroupMarkerNotSet(crbug.com/242999)!:A07023009C250000]Automatic fallback to software WebGL has been deprecated. Please use the --enable-unsafe-swiftshader flag to opt in to lower security guarantees for trusted content.
[39712:27412:0531/224849.039:ERROR:gles2_cmd_decoder_passthrough.cc(1081)] [GroupMarkerNotSet(crbug.com/242999)!:A0A023009C250000]Automatic fallback to software WebGL has been deprecated. Please use the --enable-unsafe-swiftshader flag to opt in to lower security guarantees for trusted content.
[39712:27412:0531/224849.091:ERROR:gl_utils.cc(431)] [.WebGL-0xd5c0240ce00]GL Driver Message (OpenGL, Performance, GL_CLOSE_PATH_NV, High): GPU stall due to ReadPixels
[39712:27412:0531/224849.131:ERROR:gl_utils.cc(431)] [.WebGL-0xd5c0240ce00]GL Driver Message (OpenGL, Performance, GL_CLOSE_PATH_NV, High): GPU stall due to ReadPixels
[39712:27412:0531/224849.248:ERROR:gl_utils.cc(431)] [.WebGL-0xd5c0240ce00]GL Driver Message (OpenGL, Performance, GL_CLOSE_PATH_NV, High): GPU stall due to ReadPixels
[39712:27412:0531/224849.302:ERROR:gl_utils.cc(431)] [.WebGL-0xd5c0240ce00]GL Driver Message (OpenGL, Performance, GL_CLOSE_PATH_NV, High): GPU stall due to ReadPixels (this message will no longer repeat)
Traceback (most recent call last):
File "C:\DEV\Fiverr2025\TEMPLATES\test.py", line 29, in <module>
waitWD.until(EC.element_to_be_clickable((By.XPATH, '//div[@id="bnp_btn_accept"]//a'))).click()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\DEV\.venv\selenium\Lib\site-packages\selenium\webdriver\support\wait.py", line 138, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Why is this not working with headless mode?
yeah this is a classic issue with headless Chrome + WebGL stuff. Bing Maps in particular seems to rely heavily on GPU/WebGL features, and headless mode kinda chokes on that unless you give it a bunch of specific flags.
I had the same problem, everything worked fine in normal mode, but failed in headless with a TimeoutException
and all those weird GL Driver
errors. What’s happening is headless Chrome doesn’t always play nice with hardware acceleration or WebGL stuff out of the box.
What finally worked for me was adding these flags to force it to use SwiftShader for rendering (basically a software GPU), and enabling the newer headless mode.
options.add_argument('--headless=new') # <- important!
options.add_argument('--use-gl=swiftshader')
options.add_argument('--enable-unsafe-webgpu')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--window-size=1920x1080')
Also pro tip: in headless mode, sometimes that cookie banner (#bnp_btn_accept
) just doesn't load or isn't clickable. Add a screenshot or log the page_source
so you can actually see what's going on.
driver.save_screenshot("debug.png")
print(driver.page_source)
If that doesn't work, honestly just run it in non-headless. Some sites (especially map ones like Bing, Google Maps) just aren't worth the pain unless you need full stealth/botting. Hope that helps.