ekl1pse
ekl1pse

Reputation: 643

How to set options for chromium Edge() in Selenium?

I have been following tutorials, and I came up on Firefox, and using Options(), I could set headless mode while defining browser (browser = Firefox(options=opts)), but for Firefox only. I downloaded the driver for the new chromium Edge, but I cannot seem to set the options= keyword in Edge(). Can anyone tell me how do I set the options while defining the browser?

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

opts = Options()
opts.headless = True
browser = Edge(options=opts)
#              ^^^^^

It seems there is no options keyword, and I get an error:

Traceback (most recent call last):
  File ".\tutorial.py", line 6, in <module>
    browser = Edge(options=opts)
TypeError: __init__() got an unexpected keyword argument 'options'

Any help will be appreciated. Thanks in advance!

Upvotes: 7

Views: 26276

Answers (2)

Marcelo Santos
Marcelo Santos

Reputation: 66

I am currently using Edge v118 and selenium 4.14 (note that when trying to use msedge.selenium_tools, selenium was rolled back to a previous version, so I had to uninstall it and install it again). Similar to the examples shown in Use WebDriver to automate Microsoft Edge, you can use the following:

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

import time # for demonstration purposes, not required


options = Options()
options.binary_location = r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
# An example of option you can use:
# this is something I used to disable warning messages about not logging in.
options.add_experimental_option('excludeSwitches', ['enable-logging'])

driver = webdriver.Edge(options=options)

driver.get('https://ecosia.org/')

print("Entered website")
time.sleep(5)
print("Done waiting")

driver.quit()

Upvotes: 1

CodePuppet
CodePuppet

Reputation: 126

For Edge Chromium you need to install msedge-selenium-tools package for python and then you can initialize the driver

from msedge.selenium_tools import EdgeOptions

options = EdgeOptions()
options.use_chromium = True

driver = Edge(options)

Upvotes: 8

Related Questions