PythonAnywhere 给出“FileNotFoundError”,即使我使用的是 ChromeDriver 的绝对路径

问题描述

我在 PythonAnywhere 上得到以下代码

from selenium import webdriver
import os
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import webdriverwait
from selenium.webdriver.support import expected_conditions as EC

chrome_driver = os.path.abspath('C:/Users/ross/Desktop/chromedriver.exe')
browser = webdriver.Chrome(chrome_driver)

browser.get("https://www.google.com/")
webdriverwait(browser,10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"body > div.L3eUgb > div.o3j99.ikrT4e.om7nvf > form > div:nth-child(1) > div.A8SBwf > div.RNNXgb > div > div.a4bIc > input"))).send_keys("directions 3406 north seminary ave to 933 robinhood lane")
webdriverwait(browser,"body > div.L3eUgb > div.o3j99.ikrT4e.om7nvf > form > div:nth-child(1) > div.A8SBwf > div.FPdoLc.lJ9FBc > center > input.gNO89b"))).click()
value = webdriverwait(browser,"#exp0 > div.mGSxre.Ra9ql > div > div"))).text
print(value)

我引用的是绝对路径,当我在 PyCharm 上运行它时,代码将起作用。我收到以下错误FileNotFoundError: [Errno 2] No such file or directory: '/home/rossleavitt/C:/Users/ross/Desktop/chromedriver.exe' 在处理该错误时,我得到 selenium.common.exceptions.WebDriverException: Message: 'chromedriver.exe' executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home。这里发生了什么事?我需要升级我的 PythonAnywhere 帐户吗?

罗斯

解决方法

您的问题显然出在 os.path.abspath('C:/Users/ross/Desktop/chromedriver.exe') 行上。
可以看出,它返回的 /home/rossleavitt/C:/Users/ross/Desktop/chromedriver.exe 似乎不是 chromedriver.exe 文件的有效路径
正如我从 os.path.abspath 输出 /home/rossleavitt/ 中看到的,您正在 Linux 机器上工作。
如果是这样,那么 chromedriver.exe 不可能位于此路径 C:/Users/ross/Desktop/chromedriver.exe
所以它是 Linux 和 Windows 路径的混乱组合。
如果我正确理解 os.path.abspath 的工作原理,如果您想使用 os.path.abspath,您应该将 chromedriver.exe 文件的路径添加到系统 PATH。
然后您可以简单地将它用作 os.path.abspath(chromedriver.exe),它将返回 chromedriver.exe 文件的绝对路径。

,

PythonAnywhere 上 chromedriver 的路径是 /usr/local/bin/chromedriver,但您不需要它,因为 /usr/local/bin/$PATH

Linux 上的 Chromedriver 不是 chromedriver.exe,而是 chromedriver

要在需要升级 selenium 的任何地方使用 Python 上的 Chrome。见https://help.pythonanywhere.com/pages/selenium/