我正在尝试使用Python和Selenium学习web抓取。然而,我一直收到一个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作为前缀,也有/chromedriver作为exe作为扩展名或没有。似乎什么都不起作用。我在实例化预订类时遇到了上面提到的错误
如果我像这样使用网络驱动程序,而不是使用OOP;
os.environ["PATH"] += r"/Users/username/Desktop/SeleniumDriver"
driver = webdriver.Chrome("/Users/username/Desktop/SeleniumDriver/chromedriver")
driver.get("https://website.com")
它有效,不会给我任何错误,但我更喜欢使用OOP方法,因为它对我来说更清晰,尤其是在创建机器人时
如果你使用的是Mac/Linux,位置可以
/Users/username/Desktop/SeleniumDriver/chromedriver
如果您使用的是Windows,您可能需要从实际驱动器中指定
C:/Users/username/Desktop/SeleniumDriver/chromedriver
你可以通过修改这行来尝试这个破解
super(Booking, self).__init__()
到这个
super(Booking, self).__init__(driver_path)
查看__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().__init__(executable_path = "/Users/username/Desktop/SeleniumDriver/chromedriver")