Selenium Chrome webdriver stopped working

huangapple go评论88阅读模式
英文:

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:

  1. ---------------------------------------------------------------------------
  2. TypeError Traceback (most recent call last)
  3. <ipython-input-26-c1c375032a08> in <cell line: 10>()
  4. 15
  5. 16 # Specify the path to chromedriver executable
  6. ---> 17 driver = webdriver.Chrome("chromedriver", options=options)
  7. 18 driver.get(url)
  8. 19 print(driver.title)
  9. TypeError: WebDriver.__init__() got multiple values for argument 'options'

My code is the following:

Getting Chromedriver

  1. %%shell
  2. # Ubuntu no longer distributes chromium-browser outside of snap
  3. #
  4. # Proposed solution: https://askubuntu.com/questions/1204571/how-to-install-chromium-without-snap
  5. # Add debian buster
  6. cat > /etc/apt/sources.list.d/debian.list <<'EOF'
  7. deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster.gpg] http://deb.debian.org/debian buster main
  8. deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster-updates.gpg] http://deb.debian.org/debian buster-updates main
  9. deb [arch=amd64 signed-by=/usr/share/keyrings/debian-security-buster.gpg] http://deb.debian.org/debian-security buster/updates main
  10. EOF
  11. # Add keys
  12. apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517
  13. apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
  14. apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 112695A0E562B32A
  15. apt-key export 77E11517 | gpg --dearmour -o /usr/share/keyrings/debian-buster.gpg
  16. apt-key export 22F3D138 | gpg --dearmour -o /usr/share/keyrings/debian-buster-updates.gpg
  17. apt-key export E562B32A | gpg --dearmour -o /usr/share/keyrings/debian-security-buster.gpg
  18. # Prefer debian repo for chromium* packages only
  19. # Note the double-blank lines between entries
  20. cat > /etc/apt/preferences.d/chromium.pref << 'EOF'
  21. Package: *
  22. Pin: release a=eoan
  23. Pin-Priority: 500
  24. Package: *
  25. Pin: origin "deb.debian.org"
  26. Pin-Priority: 300

Installing dependencies

  1. !apt-get update
  2. !apt-get install chromium chromium-driver
  3. !pip3 install selenium
  4. !pip install selenium
  5. !pip3 install -U selenium
  6. !api-get update
  7. !api-get install -y chromium-browser
  8. !pip3 install webdriver-manager
  9. !apt install chromium-chromedriver

The code to try to run the webdriver:

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.options import Options
  3. from selenium.common.exceptions import WebDriverException
  4. try:
  5. url = "http://example.com/"
  6. options = Options()
  7. options.add_argument("--headless")
  8. options.add_argument("--no-sandbox")
  9. driver = webdriver.Chrome("chromedriver", options=options)
  10. driver.get(url)
  11. print(driver.title)
  12. driver.quit()
  13. except WebDriverException as e:
  14. print(str(e))
  15. 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:

  1. ---------------------------------------------------------------------------
  2. TypeError Traceback (most recent call last)
  3. &lt;ipython-input-26-c1c375032a08&gt; in &lt;cell line: 10&gt;()
  4. 15
  5. 16 # Specify the path to chromedriver executable
  6. ---&gt; 17 driver = webdriver.Chrome(&quot;chromedriver&quot;, options=options)
  7. 18 driver.get(url)
  8. 19 print(driver.title)
  9. TypeError: WebDriver.__init__() got multiple values for argument &#39;options&#39;

My code is the following:

Getting Chromedriver

  1. %%shell
  2. # Ubuntu no longer distributes chromium-browser outside of snap
  3. #
  4. # Proposed solution: https://askubuntu.com/questions/1204571/how-to-install-chromium-without-snap
  5. # Add debian buster
  6. cat &gt; /etc/apt/sources.list.d/debian.list &lt;&lt;&#39;EOF&#39;
  7. deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster.gpg] http://deb.debian.org/debian buster main
  8. deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster-updates.gpg] http://deb.debian.org/debian buster-updates main
  9. deb [arch=amd64 signed-by=/usr/share/keyrings/debian-security-buster.gpg] http://deb.debian.org/debian-security buster/updates main
  10. EOF
  11. # Add keys
  12. apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517
  13. apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
  14. apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 112695A0E562B32A
  15. apt-key export 77E11517 | gpg --dearmour -o /usr/share/keyrings/debian-buster.gpg
  16. apt-key export 22F3D138 | gpg --dearmour -o /usr/share/keyrings/debian-buster-updates.gpg
  17. apt-key export E562B32A | gpg --dearmour -o /usr/share/keyrings/debian-security-buster.gpg
  18. # Prefer debian repo for chromium* packages only
  19. # Note the double-blank lines between entries
  20. cat &gt; /etc/apt/preferences.d/chromium.pref &lt;&lt; &#39;EOF&#39;
  21. Package: *
  22. Pin: release a=eoan
  23. Pin-Priority: 500
  24. Package: *
  25. Pin: origin &quot;deb.debian.org&quot;
  26. Pin-Priority: 300

Installing dependencies

  1. !apt-get update
  2. !apt-get install chromium chromium-driver
  3. !pip3 install selenium
  4. !pip install selenium
  5. !pip3 install -U selenium
  6. !api-get update
  7. !api-get install -y chromium-browser
  8. !pip3 install webdriver-manager
  9. !apt install chromium-chromdriver

The code to try to run the webdriver:

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.options import Options
  3. from selenium.common.exceptions import WebDriverException
  4. try:
  5. url = &quot;http://example.com/&quot;
  6. options = Options()
  7. options.add_argument(&quot;--headless&quot;)
  8. options.add_argument(&quot;--no-sandbox&quot;)
  9. driver = webdriver.Chrome(&quot;chromedriver&quot;, options=options)
  10. driver.get(url)
  11. print(driver.title)
  12. driver.quit()
  13. except WebDriverException as e:
  14. 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

Selenium Chrome webdriver stopped working

请注意,第一个参数不再是 executable_path,而是 options。(这就是为什么它抱怨你传递了两次的原因。)

如果你想传递一个 executable_path,现在你必须使用 service 参数。

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.service import Service
  3. service = Service(executable_path="chromedriver")
  4. options = webdriver.ChromeOptions()
  5. options.add_argument('--headless')
  6. options.add_argument('--no-sandbox')
  7. driver = webdriver.Chrome(service=service, options=options)
  8. # ...
  9. driver.quit()
英文:

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

Selenium Chrome webdriver stopped working

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.

  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.service import Service
  3. service = Service(executable_path=&quot;chromedriver&quot;)
  4. options = webdriver.ChromeOptions()
  5. options.add_argument(&#39;--headless&#39;)
  6. options.add_argument(&#39;--no-sandbox&#39;)
  7. driver = webdriver.Chrome(service=service, options=options)
  8. # ...
  9. driver.quit()

答案2

得分: 0

今天发生了同样的事情。基本上,Selenium发布了4.10版本,他们彻底删除了一堆废弃的代码,包括使用字符串路径来实例化webdriver的方式。

在这里,您可以看到正确的做法(截止到4.10版本唯一正确的方式):https://stackoverflow.com/a/70099102/9453904

基本上,您需要使用Service对象和自动化的ChromeDriverManager(pip install webdriver-manager),它会为您下载正确的webdriver:

  1. from selenium.webdriver.chrome.options import Options
  2. from selenium.webdriver.chrome.service import Service
  3. from webdriver_manager.chrome import ChromeDriverManager
  4. opts = Options()
  5. driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=opts)
  6. 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:

  1. from selenium.webdriver.chrome.options import Options
  2. from selenium.webdriver.chrome.service import Service
  3. from webdriver_manager.chrome import ChromeDriverManager
  4. opts = Options()
  5. driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=opts)
  6. 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.

huangapple
  • 本文由 发表于 2023年6月8日 14:01:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76428999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定