WebDriverException:Service U:/Scraping/chromedriver.exe意外退出。使用Chrome和Python时,状态代码为:1

时间:2018-06-13 19:35:44

标签: python google-chrome selenium selenium-webdriver selenium-chromedriver

我一直在尝试让网络驱动程序在Chrome中使用Python工作,但不管我的生活能找出错误,尽管在一天中的大部分时间都会排除故障

我已经将chromedriver解压缩到我正在工作的文件夹中。我已尝试将executable_path参数与chromedriver一起使用。我已尝试更新chromedriver中的选项以指向Chrome.exe文件。

代码如下。非常直截了当。 '网址'我在代码的前面有一个地址,我不在这里 - 脚本甚至没有做到那么远。

from selenium import webdriver

driver = webdriver.Chrome(executable_path = 'U:/Scraping/chromedriver.exe')
driver.get(url)

错误:

    Traceback (most recent call last):

  File "<ipython-input-67-db2ce2aa7cdf>", line 1, in <module>
    runfile('U:/Scraping/Project.py', wdir='U:/Scraping')

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "U:/Scraping/Project.py", line 14, in <module>
    driver = webdriver.Chrome(executable_path = 'U:/Scraping/chromedriver.exe')

  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 68, in __init__
    self.service.start()

  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\common\service.py", line 98, in start
    self.assert_process_still_running()

  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\common\service.py", line 111, in assert_process_still_running
    % (self.path, return_code)

WebDriverException: Service U:/Scraping/chromedriver.exe unexpectedly exited. Status code was: 1

3 个答案:

答案 0 :(得分:1)

在通过参数 executable_path 传递 ChromeDriver 二进制文件的绝对路径时,您需要提及路径在单引号内(即'')由单个正斜杠(即\)与原始开关(即r)分开,如下所示:

from selenium import webdriver

driver = webdriver.Chrome(executable_path=r'U:\Scraping\chromedriver.exe')
driver.get(url)

其他考虑因素

  • 确保您已从以下基础操作系统的download location下载 ChromeDriver 二进制文件的确切格式:

    • chromedriver_win32.zip :适用于 Windows操作系统
  • 确保 ChromeDriver 二进制文件具有非管理员用户的可执行权限。

  • 非管理员用户身份执行测试
  • 错误的另一个可能原因可能是错过 127.0.0.1 localhost
  • 中的条目 /etc/hosts

解决方案

  • Windows操作系统 - 将127.0.0.1 localhost添加到/etc/hosts

  • Mac OSX - 确保以下条目:

    127.0.0.1   localhost
    255.255.255.255 broadcasthost
    ::1 localhost
    fe80::1%lo0 localhost   
    

参考

根据selenium.common.exceptions.WebDriverException: Message: Can not connect to the Service geckodriver中的讨论:

  • Selenium不需要在主机文件中明确设置127.0.0.1 localhost
  • 但是,必须要求 localhost 映射到 IPv4本地环回(127.0.0.1)
  • 此映射的机制不必始终通过hosts文件。
  • Windows操作系统系统上,它根本没有映射到hosts文件中(解析localhost由DNS解析器完成)。

TL; DR

How to reset the Hosts file back to the default

答案 1 :(得分:1)

我和@rvictordelta有相似的经历。出于某种原因,我无法再通过python编辑驱动程序所在的位置,并且当我更改为共享驱动器进行工作时也无法正常工作。最后,在下面使用此代码。这个版本很好,因为它会检查最新的chrome驱动程序。如果驱动程序存在,则仅使用它,否则,它将下载并安装。

custom_path=r'C:\Users\username'

driver = webdriver.Chrome(ChromeDriverManager(path=custom_path).install(),options=chrome_options))

答案 2 :(得分:0)

这里有同样的错误。我的问题是我有chromedriver.exe个公司股票。某些防火墙或安全设置可能阻止了python访问该远程位置的可执行文件。

我将chromedriver.exe设为本地,并且有效。