How to run headless Microsoft Edge with Selenium in Python?
W

4

7

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?

Wynnie answered 6/12, 2020 at 17:51 Comment(2)
what version of selenium are you using?Zellner
@Zellner Selenium 3.141.0Wynnie
T
7
  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)
Transpacific answered 6/12, 2020 at 18:12 Comment(8)
I get AttributeError: 'Options' object has no attribute 'AddArgument'Wynnie
It was java code before , added python code and the edge chromium documentation . If you are using edge legacy then you cannot run it in headlessTranspacific
I running the latest version of edge and still get the same errorWynnie
What error , I updated the code could you paste the errorTranspacific
pip install msedge-selenium-tools , you have to install this read that documentation .Transpacific
The fist thing I get is EdgeOptions is not defined and then an error saying AttributeError: 'Options' object has no attribute 'add_argument' Edit: I already installed msedge-toolsWynnie
The code now works but I still need to insatiate the driver and I can't add the options as a parameter.Wynnie
anyone knows how to do this in NodeJS?Heptateuch
H
3

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.

Hulburt answered 21/3, 2021 at 20:42 Comment(0)
H
1

You can run headless Microsoft Edge with Selenium in Python as shown below:

from selenium import webdriver

options = webdriver.EdgeOptions()
options.add_argument("--headless=new") # Here
driver = webdriver.Edge(options=options)

Or:

from selenium import webdriver
from selenium.webdriver.edge.options import Options

options = Options()
options.add_argument("--headless=new") # Here
driver = webdriver.Edge(options=options)

In addition, the examples below can test Django Admin with headless Microsoft Edge, Selenium, pytest-django and Django. *My answer explains how to test Django Admin with multiple headless browsers(Chrome, Microsoft Edge and Firefox), Selenium, pytest-django and Djang:

# "tests/test_1.py"

import pytest
from selenium import webdriver
from django.test import LiveServerTestCase

@pytest.fixture(scope="class")
def edge_driver_init(request):
    options = webdriver.EdgeOptions()
    options.add_argument("--headless=new")
    edge_driver = webdriver.Edge(options=options)
    request.cls.driver = edge_driver
    yield
    edge_driver.close()

@pytest.mark.usefixtures("edge_driver_init")
class Test_URL_Edge(LiveServerTestCase):
    def test_open_url(self):
        self.driver.get(("%s%s" % (self.live_server_url, "/admin/")))
        assert "Log in | Django site admin" in self.driver.title

Or:

# "tests/conftest.py"

import pytest
from selenium import webdriver

@pytest.fixture(scope="class")
def edge_driver_init(request):
    options = webdriver.EdgeOptions()
    options.add_argument("--headless=new")
    edge_driver = webdriver.Edge(options=options)
    request.cls.driver = edge_driver
    yield
    edge_driver.close()
# "tests/test_1.py"

import pytest
from django.test import LiveServerTestCase

@pytest.mark.usefixtures("edge_driver_init")
class Test_URL_Edge(LiveServerTestCase):
    def test_open_url(self):
        self.driver.get(("%s%s" % (self.live_server_url, "/admin/")))
        assert "Log in | Django site admin" in self.driver.title
Hillery answered 3/9, 2023 at 15:8 Comment(0)
N
-2

for Edge browser

options = EdgeOptions()

options.use_chromium = True

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

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

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

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')

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

wd.maximize_window()
Nealneala answered 28/5, 2021 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.