pythonselenium-webdrivermicrosoft-edgemicrosoft-edge-headlessbrowser-options

How to run headless Microsoft Edge with Selenium in Python?


With Chrome you can add options when creating the driver. You just do

options = Options()
options.headless = True
driver = webdriver.Chrome(PATH\TO\DRIVER, options=options)

But for some reason when trying to do the same with Microsoft Edge

options = Options()
options.headless = True
driver = webdriver.Edge(PATH\TO\DRIVER, options=options)

I get this error below:

TypeError: init() got an unexpected keyword argument 'options'

For some reason Edge's driver doesn't accept any other parameters than the file path. Is there any way to run Edge headless and add more options just like in Chrome?


Solution

  •   options = EdgeOptions()
      options.use_chromium = True
      options.add_argument("headless")
      options.add_argument("disable-gpu")
    

    Try above code , you have to enable chromium to enable headless

    https://learn.microsoft.com/en-us/microsoft-edge/webdriver-chromium/?tabs=python

    This works only for new edge chromium not for edge legacy versions . In legacy versions headless is not supported

    Full code

    from msedge.selenium_tools import EdgeOptions
    from msedge.selenium_tools import Edge
    
    # make Edge headless
    edge_options = EdgeOptions()
    edge_options.use_chromium = True  # if we miss this line, we can't make Edge headless
    # A little different from Chrome cause we don't need two lines before 'headless' and 'disable-gpu'
    edge_options.add_argument('headless')
    edge_options.add_argument('disable-gpu')
    driver = Edge(executable_path='youredgedriverpath', options=edge_options)