[Python] Opening site only possible in non-headless mode?

Discussão em 'Python' iniciado por Stack, Maio 31, 2025.

  1. Stack

    Stack Membro Participativo

    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?

    Continue reading...

Compartilhe esta Página