【发布时间】:2019-05-01 02:31:54
【问题描述】:
我试图使用 Selenium 运行无头 Chrome 浏览器以从网络上抓取内容。我使用 wget 安装了无头 Chrome,然后在我当前的文件夹中解压缩。
!wget "http://chromedriver.storage.googleapis.com/2.25/chromedriver_linux64.zip"
!unzip chromedriver_linux64.zip
现在我正在加载驱动程序
from selenium.webdriver.chrome.options import Options
import os
# instantiate a chrome options object so you can set the size and headless preference
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
chrome_driver = os.getcwd() +"/chromedriver"
driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=chrome_driver)
我遇到了一个错误
WebDriverException Traceback (most recent call last)
<ipython-input-67-0aeae0cfd891> in <module>()
----> 1 driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
2 driver.get("https://www.google.com")
3 lucky_button = driver.find_element_by_css_selector("[name=btnI]")
4 lucky_button.click()
5 /usr/local/lib/python3.6/dist-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, chrome_options, service_args, desired_capabilities, service_log_path)
60 service_args=service_args,
61 log_path=service_log_path)
---> 62 self.service.start()
63
64 try:
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/service.py in start(self)
84 count = 0
85 while True:
---> 86 self.assert_process_still_running()
87 if self.is_connectable():
88 break
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/service.py in assert_process_still_running(self)
97 raise WebDriverException(
98 'Service %s unexpectedly exited. Status code was: %s'
---> 99 % (self.path, return_code)
100 )
101
WebDriverException: Message: Service /content/chromedriver unexpectedly exited. Status code was: -6
更新
所以经过一番研究,我尝试了另一种方式
!apt install chromium-chromedriver
import selenium as se
options = se.webdriver.ChromeOptions()
options.add_argument('headless')
driver = se.webdriver.Chrome(chrome_options=options)
在 Google Colab 上再次给我同样的错误
WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: -6
【问题讨论】:
-
@DebanjanB 这三个问题没有与此问题相关的答案。操作系统不同(Win 与 Linux),没有公认的答案。
-
没有一个答案与错误代码-6有关。我尝试了所有方法。在阅读整个问题之前,请不要标记重复。
-
@KorakotChaovavanich 主要错误是 chromedriver 意外退出。不同的OS 和不同的Selenium Language Binding Arts 将针对相同的错误显示不同的
Status code。我已将 OP 指向最相关的讨论。如果您还有其他疑虑,请告诉我。 -
美汤不能刮掉javascript生成的内容
-
请分享一个能重现您观察到的问题的笔记本。
标签: python selenium google-chrome selenium-webdriver google-colaboratory