使用python在硒中打开Goog​​le Chrome时出错

时间:2019-08-17 22:21:15

标签: python selenium web-scraping

我已经尝试了许多关于stackoverflow的解决方案,但是没有一个起作用。

我想在Windows上使用ubuntu shell在python中使用硒打开谷歌浏览器,但是每当出现错误时,我都会出错。 Terminal where I run the code as well as the folder path

我试图通过在webdriver.Chrome函数中指定路径来做到这一点,

webdriver.Chrome('/path/to/chromedriver.exe') 

但相同的错误不断发生,

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 76, in start
    stdin=PIPE)
  File "/usr/lib/python3.5/subprocess.py", line 947, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child
    raise child_exception_type(errno_num, err_msg)
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/vinve/Desktop/chromedriver_win32/chromedriver.exe'

在处理上述异常期间,发生了另一个异常:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/chrome/webdriver.py", line 73, in __init__
    self.service.start()
  File "/usr/local/lib/python3.5/dist-packages/selenium/webdriver/common/service.py", line 83, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home

我尝试使用双斜杠和原始字符串键入路径,但是没有任何效果。

我也将chromedriver.exe文件添加到scripts文件夹,但这没有帮助。我也运行了没有.exe的webdriver.Chrome(/ path / to / chromedriver)路径,但没有用。我也尝试在--headless模式下使用chrome_options运行它,但是没有用。 (所有这些选项在上面都给出了相同的错误)

编辑: 回答: 我想到了!事实是,尽管我使用的是Windows 10,但我使用的是Windows Subsystem for Linux(Ubuntu shell)。因此,在webdriver.Chrome(/path/to/chromedriver.exe)中指定的路径不应以C:/path/to/driver.exe开头,而应为/ mnt / c / Users / path / to / chromedriver.exe

1 个答案:

答案 0 :(得分:0)

应为webdriver.Chrome('/path/to/chromedriver'),不带.exe

相关问题