我正在嘗試使用 Python 和 Selenium 學習網路抓取。但是,我遇到了一個錯誤,FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'并且我 100% 確定該檔案位于我指定的路徑上,因為我之前嘗試過使用更簡單的方法,并且一切正常。
這是我現在的代碼;
class Booking(webdriver.Chrome):
def __init__(self, driver_path=r"/Users/username/Desktop/SeleniumDriver/chromedriver"):
self.driver_path = driver_path
os.environ["PATH"] = r"/Users/username/Desktop/SeleniumDriver"
super(Booking, self).__init__()
def land_first_page(self):
self.get("https://website.com")
inst = Booking()
inst.land_first_page()
我已經嘗試了許多不同的路徑,帶有/不帶有 r 作為前綴,以及帶有 exe 作為擴展名或不帶有 /chromedriver 的路徑。似乎沒有任何作業。我在實體化 Booking 類時收到我上面提到的錯誤
如果我像這樣使用 webdriver 而不是使用 OOP;
os.environ["PATH"] = r"/Users/username/Desktop/SeleniumDriver"
driver = webdriver.Chrome("/Users/username/Desktop/SeleniumDriver/chromedriver")
driver.get("https://website.com")
它有效,并且不會給我任何錯誤,但我更喜歡使用 OOP 方法,因為它對我來說更清晰,尤其是在創建機器人時
uj5u.com熱心網友回復:
如果您使用的是 Mac/Linux,位置還可以
/Users/username/Desktop/SeleniumDriver/chromedriver
如果您使用的是 Windows,則可能需要從實際驅動器中指定
C:/Users/username/Desktop/SeleniumDriver/chromedriver
uj5u.com熱心網友回復:
您可以通過修改此行來嘗試此 hack
super(Booking, self).__init__()
對此
super(Booking, self).__init__(driver_path)
uj5u.com熱心網友回復:
查看該__init__方法的源代碼,那里沒有初始化實體變數。我的意思是,例如沒有像self.driver_path = "path".
def __init__(self, executable_path="chromedriver", port=0,
options=None, service_args=None,
desired_capabilities=None, service_log_path=None,
chrome_options=None, keep_alive=True):
"""
Creates a new instance of the chrome driver.
Starts the service and then creates new instance of chrome driver.
:Args:
- executable_path - path to the executable. If the default is used it assumes the executable is in the $PATH
- port - port you would like the service to run, if left as 0, a free port will be found.
- options - this takes an instance of ChromeOptions
- service_args - List of args to pass to the driver service
- desired_capabilities - Dictionary object with non-browser specific
capabilities only, such as "proxy" or "loggingPref".
- service_log_path - Where to log information from the driver.
- chrome_options - Deprecated argument for options
- keep_alive - Whether to configure ChromeRemoteConnection to use HTTP keep-alive.
"""
if chrome_options:
warnings.warn('use options instead of chrome_options',
DeprecationWarning, stacklevel=2)
options = chrome_options
if options is None:
# desired_capabilities stays as passed in
if desired_capabilities is None:
desired_capabilities = self.create_options().to_capabilities()
else:
if desired_capabilities is None:
desired_capabilities = options.to_capabilities()
else:
desired_capabilities.update(options.to_capabilities())
self.service = Service(
executable_path,
port=port,
service_args=service_args,
log_path=service_log_path)
self.service.start()
try:
RemoteWebDriver.__init__(
self,
command_executor=ChromeRemoteConnection(
remote_server_addr=self.service.service_url,
keep_alive=keep_alive),
desired_capabilities=desired_capabilities)
except Exception:
self.quit()
raise
self._is_remote = False
def launch_app(self, id):
"""Launches Chrome app specified by id."""
return self.execute("launchApp", {'id': id})
def get_network_conditions(self):
"""
Gets Chrome network emulation settings.
:Returns:
A dict. For example:
{'latency': 4, 'download_throughput': 2, 'upload_throughput': 2,
'offline': False}
"""
return self.execute("getNetworkConditions")['value']
所以在你的代碼中,這條線self.driver_path = driver_path沒有做任何事情。它設定了一個不被父類使用的實體變數。
您可以在 super 陳述句中傳遞路徑以使其作業:
super().__init__(executable_path = "/Users/username/Desktop/SeleniumDriver/chromedriver")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/376552.html
標籤:Python 蟒蛇-3.x 硒 硒网络驱动程序 硒铬驱动器
