skip to Main Content

I have a script in google Colab that uses Selenium for automating some Chrome tasks. The weird thing is that this script worked perfectly until today. Suddenly, when I run my script, I get this error that I did not get before:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-26-c1c375032a08> in <cell line: 10>()
     15 
     16     # Specify the path to chromedriver executable
---> 17     driver = webdriver.Chrome("chromedriver", options=options)
     18     driver.get(url)
     19     print(driver.title)

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

My code is the following:

Getting Chromedriver

%%shell
# Ubuntu no longer distributes chromium-browser outside of snap
#
# Proposed solution: https://askubuntu.com/questions/1204571/how-to-install-chromium-without-snap

# Add debian buster
cat > /etc/apt/sources.list.d/debian.list <<'EOF'
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster.gpg] http://deb.debian.org/debian buster main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster-updates.gpg] http://deb.debian.org/debian buster-updates main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-security-buster.gpg] http://deb.debian.org/debian-security buster/updates main
EOF

# Add keys
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 112695A0E562B32A

apt-key export 77E11517 | gpg --dearmour -o /usr/share/keyrings/debian-buster.gpg
apt-key export 22F3D138 | gpg --dearmour -o /usr/share/keyrings/debian-buster-updates.gpg
apt-key export E562B32A | gpg --dearmour -o /usr/share/keyrings/debian-security-buster.gpg

# Prefer debian repo for chromium* packages only
# Note the double-blank lines between entries
cat > /etc/apt/preferences.d/chromium.pref << 'EOF'
Package: *
Pin: release a=eoan
Pin-Priority: 500


Package: *
Pin: origin "deb.debian.org"
Pin-Priority: 300

Installing dependencies

!apt-get update
!apt-get install chromium chromium-driver
!pip3 install selenium

!pip install selenium
!pip3 install -U selenium
!api-get update
!api-get install -y chromium-browser
!pip3 install webdriver-manager
!apt install chromium-chromdriver

The code to try to run the webdriver:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.common.exceptions import WebDriverException

try:
  url = "http://example.com/"
  options = Options()
  options.add_argument("--headless")
  options.add_argument("--no-sandbox")
  driver = webdriver.Chrome("chromedriver", options=options)
  driver.get(url)
  print(driver.title)
  driver.quit()
except WebDriverException as e:
  print(str(e))

Refactoring my code to remove the initial parameter "chromedriver" seems to work, but I do not understand why the code worked perfectly before and not now.

2

Answers


  1. The same happened to me today. Basically Selenium released version 4.10 and they totally removed a bunch of deprecated code, including such way of instantiating the webdriver with a string path to the driver.

    Here you can see what is the correct way of doing so (and only one as of 4.10): https://stackoverflow.com/a/70099102/9453904

    Basically you have to use the Service object, and the automated ChromeDriverManager (pip install webdriver-manager) which will download the correct webdriver for you:

    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.chrome.service import Service
    from webdriver_manager.chrome import ChromeDriverManager
    
    opts = Options()
    driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=opts)
    
    driver.get(url)
    
    Login or Signup to reply.
  2. 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="chromedriver")
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    driver = webdriver.Chrome(service=service, options=options)
    # ...
    driver.quit()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search