Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 941
Maypher Avatar asked Dec 06 '20 17:12

Maypher


People also ask

How do I run Edge browser in headless mode?

It is quite simple to run your tests in the headless mode. You need simply to add the "--headless" argument to the EdgeOptions object. NOTE: Make sure to download the correct version of Edge driver and copy it to the output folder. Also, you need to install the latest version of the Microsoft.

Does Selenium support Microsoft Edge?

Selenium WebDriver is an open-source testing framework that can be used on any platform, and provides language bindings for Java, Python, C#, Ruby, and JavaScript. To use WebDriver to automate Microsoft Edge, if you use Selenium, you must use Selenium 4, which has built-in support for Microsoft Edge (Chromium).

Does Selenium run headless?

Yes, Selenium supports headless testing. In older versions of Selenium, we used the HTMLUnitDriver mainly, a headless driver providing a Non-GUI implementation of Selenium WebDriver.

How do I download Edge WebDriver for Selenium?

Open Microsoft Edge Webdriver page using this link – https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/ Click on Downloads and from the list of different versions of Edge drivers, download the latest one or as per your requirement. This will download the msi file.


Video Answer


2 Answers

  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)
like image 128
PDHide Avatar answered Oct 09 '22 05:10

PDHide


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.

like image 24
s13rw81 Avatar answered Oct 09 '22 03:10

s13rw81