TypeError: WebDriver.__init__() got multiple values for argument 'options'
Asked Answered
C

3

26

Error is:

TypeError: WebDriver.__init__() got multiple values for argument 'options'

`

The code is:

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

browser = webdriver.Chrome(r'/usr/bin/chromedriver', options=chrome_options)

This is the error:

TypeError                                 Traceback (most recent call last)
<ipython-input-5-9a7e59e392ae> in <cell line: 6>()
      4 chrome_options.add_argument('--headless')
      5 
----> 6 browser = webdriver.Chrome(r'/usr/bin/chromedriver', options=chrome_options)

TypeError: WebDriver.__init__() got multiple values for argument 'options'
Cork answered 8/6, 2023 at 4:11 Comment(1)
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.Doubler
N
59

This is due to changes in selenium 4.10.0: https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e

Changes_in_selenium_4_10_0

Note that the first argument is no longer executable_path, but options. (That's why it complains that you're passing it in twice.)

If you want to pass in an executable_path, you'll have to use the service arg now.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service

service = Service(executable_path=r'/usr/bin/chromedriver')
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(service=service, options=options)
# ...
driver.quit()
Nemathelminth answered 8/6, 2023 at 13:25 Comment(6)
If anyone else finds their way here because pyhtml2pdf was affected by this breakage, there's a pull request based on this answer that fixes the problem: github.com/kumaF/pyhtml2pdf/pull/2Beare
Thanks, Michael. Using your suggestion I'm getting: WebDriverException: Message: Service /usr/bin/chromedriver unexpectedly exited. Status code was: 1 Any thoughts on thisAutocatalysis
@OmarSaidLópezTronco That looks like a separate issue: https://mcmap.net/q/278034/-webdriverexception-message-service-chromedriver-unexpectedly-exited-status-code-was-127. Could also happen if Chrome isn't installed on that system.Nemathelminth
What about desired_capabilities?Afghan
@Afghan Use options.set_capability(name, value) instead of desired_capabilities.Nemathelminth
Changing driver = webdriver.Chrome(service=service, options=options) to driver = webdriver.Chrome(options=webdriver_options) worked for me!Wyne
H
1

#Working solution for selenium driver not working

from selenium import webdriver

from selenium.webdriver.chrome.service import Service

Please cross check if you add .exe at the end of your Chromedriver path

service = Service(executable_path=r"..\chromedriver.exe") 

driver = webdriver.Chrome(service=service)

#It work for me hope selenium version 4.1 chrome 116 latest one

Horal answered 11/9, 2023 at 12:38 Comment(0)
W
1

I got this answer from this blog and it worked! https://nariyoo.com/python-how-to-run-selenium-in-google-colab/

[Python] How to run selenium in Google Colab

!pip install chromedriver-autoinstaller

import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')

import time
import pandas as pd
from bs4 import BeautifulSoup
from selenium import webdriver
import chromedriver_autoinstaller

# setup chrome options
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless') # ensure GUI is off
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')

# set path to chromedriver as per your configuration
chromedriver_autoinstaller.install()

# set the target URL
url = "put-url-here-to-scrape"

# set up the webdriver
driver = webdriver.Chrome(options=chrome_options)

Now you can easily import other selenium library that you need and the driver will be worked. I hope it help!

Witwatersrand answered 4/12, 2023 at 5:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.