9

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?

2
  • what version of selenium are you using? Commented Dec 6, 2020 at 18:02
  • @Sharmiko Selenium 3.141.0 Commented Dec 6, 2020 at 18:06

4 Answers 4

9
  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)
Sign up to request clarification or add additional context in comments.

8 Comments

I get AttributeError: 'Options' object has no attribute 'AddArgument'
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 headless
I running the latest version of edge and still get the same error
What error , I updated the code could you paste the error
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-tools
|
4

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.

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.