简体   繁体   中英

How to run Microsoft Edge headless with Selenium 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

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?

  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)

webdriver.Edge does not accept any options so i switched it to the following: It worked for me.

        # imports
        from selenium import webdriver
        from msedge.selenium_tools import EdgeOptions

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

        browser = webdriver.Chrome(
            executable_path="resources/msedgedriver.exe", options=options)
        browser.get(gen_token)

The version of Microsoft Edge that I am using is:

Microsoft Edge Version 89.0.774.57 (Official build) (64-bit)

This worked for me.

for Edge browser

options = EdgeOptions()

options.use_chromium = True

options.add_argument('--allow-running-insecure-content')

options.add_argument("--ignore-certificate-errors")

self.wd = webdriver.Chrome(executable_path=EdgeChromiumDriverManager().install(), options=options)

self.wd.maximize_window()

For Edge headless

options = EdgeOptions()

options.use_chromium = True

options.add_argument("--headless")

options.add_argument("disable-gpu")

options.add_argument('--allow-running-insecure-content')

options.add_argument('--ignore-certificate-errors')

self.wd = webdriver.Chrome(executable_path=EdgeChromiumDriverManager().install(), options=options)

self.wd.maximize_window()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM