首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Python Selenium找不到chromedriver

Python Selenium找不到chromedriver
EN

Stack Overflow用户
提问于 2021-12-04 11:11:23
回答 3查看 108关注 0票数 0

我正在尝试用Python和Selenium学习web抓取。然而,我得到了一个FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'的错误,我100%确定文件位于我指定的路径上,因为我之前用更简单的方法尝试了它,一切都很好。

这是我现在的代码;

代码语言:javascript
运行
复制
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。似乎什么都不起作用。在实例化Booking类时,我得到了上面提到的错误

如果我像这样使用webdriver,而不是使用OOP;

代码语言:javascript
运行
复制
os.environ["PATH"] += r"/Users/username/Desktop/SeleniumDriver"

driver = webdriver.Chrome("/Users/username/Desktop/SeleniumDriver/chromedriver")
driver.get("https://website.com")

它可以工作,不会给我任何错误,但我更喜欢使用OOP方法,因为它对我来说更容易使用,特别是在创建机器人时

EN

回答 3

Stack Overflow用户

发布于 2021-12-04 11:44:09

如果您使用的是Mac/Linux,则位置也可以

代码语言:javascript
运行
复制
/Users/username/Desktop/SeleniumDriver/chromedriver

如果您使用的是Windows,则可能需要从实际驱动器指定

代码语言:javascript
运行
复制
C:/Users/username/Desktop/SeleniumDriver/chromedriver
票数 0
EN

Stack Overflow用户

发布于 2021-12-04 12:31:43

您可以通过修改此行来尝试此技巧

代码语言:javascript
运行
复制
super(Booking, self).__init__()

到这个

代码语言:javascript
运行
复制
super(Booking, self).__init__(driver_path)
票数 0
EN

Stack Overflow用户

发布于 2021-12-04 13:18:53

查看__init__方法的源代码,其中没有初始化任何实例变量。我的意思是,举个例子,没有任何东西像self.driver_path = "path"

代码语言:javascript
运行
复制
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行没有做任何事情。它设置父类不使用的实例变量。

您可以在超级语句中传递路径以使其工作:

代码语言:javascript
运行
复制
super().__init__(executable_path = "/Users/username/Desktop/SeleniumDriver/chromedriver")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70225059

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档