Home > Software design >  How can I run Edge in headless mode in the latest version of Selenium?
How can I run Edge in headless mode in the latest version of Selenium?

Time:05-22

I am working with the following selenium code on selenium 4.1.5:

from selenium.webdriver.edge.service import Service
from selenium import webdriver

edge_path = r'C:\Users\A\Desktop\Automated\msedgedriver.exe'
service = Service(executable_path=edge_path)
driver = webdriver.Edge(service = service)
driver.get('https://bing.com')

In the older version of selenium when Edge was not supported and we had to install msedge.selenium_tools package it would be written like this:

from msedge.selenium_tools import EdgeOptions
from msedge.selenium_tools import Edge
    
options= EdgeOptions()
options.use_chromium=True
options.add_argument("headless")
options.add_argument("disable-gpu")

driver=Edge(executable_path=r'C:\Users\A\Desktop\Automated\msedgedriver.exe', options=options)
driver.get('https://bing.com')

In the current version of selenium Edge is already incorporated in it (no need for msedge.selenium_tools) but I'm not sure what the syntax would be for running it in headless mode. I've tried a few ways with no luck. Any help would be greatly appreciated.

CodePudding user response:

With selenium 4.1.5, Python, and the latest version of msedgedriver to match your version of Edge:

from selenium import webdriver
options = webdriver.EdgeOptions()
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option("useAutomationExtension", False)
options.add_argument("--headless")
driver = webdriver.Edge(options=options)
driver.quit()
  • Related