英文:
Selenium Chrome webdriver stopped working
问题
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-chromedriver
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.
英文:
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.
答案1
得分: 1
这是由于 selenium
4.10.0
中的更改:
https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e
请注意,第一个参数不再是 executable_path
,而是 options
。(这就是为什么它抱怨你传递了两次的原因。)
如果你想传递一个 executable_path
,现在你必须使用 service
参数。
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()
英文:
This is due to changes in selenium
4.10.0
:
https://github.com/SeleniumHQ/selenium/commit/9f5801c82fb3be3d5850707c46c3f8176e3ccd8e
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()
答案2
得分: 0
今天发生了同样的事情。基本上,Selenium发布了4.10版本,他们彻底删除了一堆废弃的代码,包括使用字符串路径来实例化webdriver的方式。
在这里,您可以看到正确的做法(截止到4.10版本唯一正确的方式):https://stackoverflow.com/a/70099102/9453904
基本上,您需要使用Service对象和自动化的ChromeDriverManager(pip install webdriver-manager
),它会为您下载正确的webdriver:
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)
英文:
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)
答案3
得分: 0
I had a similar problem to you, in that I used the Debian Buster method, sourced from a variety of internet sources. However, even this method has stopped working for me as of late. I used the following comment section to fix the issues I had: Link. If you scroll all the way to the bottom, people have been posting updates on code that lets them use selenium with google colab. As of today, (8/16), the code they had at the bottom works fine.
英文:
I had a similar problem to you, in that I used the Debian Buster method, sourced from a variety of internet sources. However, even this method has stopped working for me as of late. I used the following comment section to fix the issues I had : Link . If you scroll all the way to the bottom, people have been posting updates on code that lets them use selenium with google colab. As of today, (8/16), the code they had at the bottom works fine.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论