当前位置:网站首页>selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT

selenium. common. exceptions. WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT

2022-04-23 20:10:00 liulanba

Need to use recently selenium, It reported an error as soon as it was running ...
The premise to prepare :
1. install selenium
2. download chrome Corresponding version chromedriver
The code is a simple demo:

from selenium import webdriver
import time

browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')
time.sleep(10)

Operation error reporting :
 Error description
The Internet said that we should chromedriver Put the environment variable , Put it in or report an error !!
Then look directly at the source code :
The process is cumbersome , It's boring , It's too much nonsense. Please turn directly to the end of the text. The solution ~~
The process is cumbersome , It's boring , It's too much nonsense. Please turn directly to the end of the text. The solution ~~
The process is cumbersome , It's boring , It's too much nonsense. Please turn directly to the end of the text. The solution ~~
1. Code error entry :

browser = webdriver.Chrome()

2. First, read the wrong place to hang the newspaper :

File “E:\APP_Install\python_install\lib\site-packages\selenium\webdriver\common\service.py”, line 83, in start
os.path.basename(self.path), self.start_error_message)

The corresponding code :

    def start(self):
        """ Starts the Service. :Exceptions: - WebDriverException : Raised either when it can't start the service or when it can't connect to the service """
        try:
            cmd = [self.path]
            cmd.extend(self.command_line_args())
            self.process = subprocess.Popen(cmd, env=self.env,
                                            close_fds=system() != 'Windows',
                                            stdout=self.log_file,
                                            stderr=self.log_file,
                                            stdin=PIPE,
                                            creationflags=self.creationflags)
        except TypeError:
            raise
        except OSError as err:
            if err.errno == errno.ENOENT:
                raise WebDriverException(
                    "'%s' executable needs to be in PATH. %s" % (
                        os.path.basename(self.path), self.start_error_message)
                )
            elif err.errno == errno.EACCES:
                raise WebDriverException(
                    "'%s' executable may have wrong permissions. %s" % (
                        os.path.basename(self.path), self.start_error_message)
                )
            else:
                raise

3. yes start() Methodical try failed , You can see here is self.path The problem of , Then look back and call start Method place ,self.path = executable Find the value transfer executable The place of :

File “E:\APP_Install\python_install\lib\site-packages\selenium\webdriver\chromium\webdriver.py”, line 90, in init
self.service.start()

The relevant code is as follows :


class ChromiumDriver(RemoteWebDriver):
    """ Controls the WebDriver instance of ChromiumDriver and allows you to drive the browser. """

    def __init__(self, browser_name, vendor_prefix,
                 port=DEFAULT_PORT, options: BaseOptions = None, service_args=None,
                 desired_capabilities=None, service_log_path=DEFAULT_SERVICE_LOG_PATH,
                 service: Service = None, keep_alive=DEFAULT_KEEP_ALIVE):
        if desired_capabilities:
            warnings.warn('desired_capabilities has been deprecated, please pass in a Service object',
                          DeprecationWarning, stacklevel=2)
        if port != DEFAULT_PORT:
            warnings.warn('port has been deprecated, please pass in a Service object',
                          DeprecationWarning, stacklevel=2)
        self.port = port
        if service_log_path != DEFAULT_SERVICE_LOG_PATH:
            warnings.warn('service_log_path has been deprecated, please pass in a Service object',
                          DeprecationWarning, stacklevel=2)
        if keep_alive != DEFAULT_KEEP_ALIVE and type(self) == __class__:
            warnings.warn('keep_alive has been deprecated, please pass in a Service object',
                          DeprecationWarning, stacklevel=2)
        else:
            keep_alive = True

        self.vendor_prefix = vendor_prefix

        _ignore_proxy = None
        if not options:
            options = self.create_options()

        if desired_capabilities:
            for key, value in desired_capabilities.items():
                options.set_capability(key, value)

        if options._ignore_local_proxy:
            _ignore_proxy = options._ignore_local_proxy

        if not service:
            raise AttributeError('service cannot be None')

        self.service = service
        self.service.start()

4. At first glance, there seems to be no problem ,self.service.start() There is no special definition and treatment in the front , There is no transfer of parameters "self.path", This is the time to consider python The problem of inheritance , I enter its parent class RemoteWebDriver There is no problem causing the error report , At this time, we need to consider subclasses , We also ignore a problem at this time : Error entry : browser = webdriver.Chrome()

webdriver.Chrome() The code is as follows :

E:\APP_Install\python_install\Lib\site-packages\selenium\webdriver\chrome\webdriver.py


class WebDriver(ChromiumDriver):
    def __init__(self, executable_path=DEFAULT_EXECUTABLE_PATH, port=DEFAULT_PORT,
                 options: Options = None, service_args=None,
                 desired_capabilities=None, service_log_path=DEFAULT_SERVICE_LOG_PATH,
                 chrome_options=None, service: Service = None, keep_alive=DEFAULT_KEEP_ALIVE):
        if executable_path != 'chromedriver':
            warnings.warn('executable_path has been deprecated, please pass in a Service object',
                          DeprecationWarning, stacklevel=2)
        if chrome_options:
            warnings.warn('use options instead of chrome_options',
                          DeprecationWarning, stacklevel=2)
            options = chrome_options
        if keep_alive != DEFAULT_KEEP_ALIVE:
            warnings.warn('keep_alive has been deprecated, please pass in a Service object',
                          DeprecationWarning, stacklevel=2)
        else:
            keep_alive = True
        if not service:
            service = Service(executable_path, port, service_args, service_log_path)

        super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
                                        port, options,
                                        service_args, desired_capabilities,
                                        service_log_path, service, keep_alive)

5. It's connected , The subclass calls the __init__ Method :

super(WebDriver, self).__init__ # And the 3 Step connection 

6. At this time, we should pay attention to Service Class reference , namely super(WebDriver, self).__init__ The penultimate parameter of the service:

service = Service(executable_path, port, service_args, service_log_path)

The corresponding code :

E:\APP_Install\python_install\Lib\site-packages\selenium\webdriver\chrome\service.py

class Service(service.ChromiumService):
    def __init__(self, executable_path: str = DEFAULT_EXECUTABLE_PATH,
                 port: int = 0, service_args: List[str] = None,
                 log_path: str = None, env: dict = None):
        super(Service, self).__init__(
            executable_path,
            port,
            service_args,
            log_path,
            env,
            "Please see https://chromedriver.chromium.org/home")

Some students here may have questions ?
Here Service And the 2 Step by step Service Not a class !
Yes , There are also two levels of nesting , direct ctrl Click in to see :

1.super(Service, self).__init__
2.service.Service.__init__(self, executable_path, port=port, env=env, start_error_message=start_error_message)

7. According to the first 6 The relationship between steps we know : In the 2 Step Service problematic self.path That's what's in it executable_path, Let's look at the definition of this parameter :

executable_path=DEFAULT_EXECUTABLE_PATH

8. Let's look at the definition of this constant :

DEFAULT_EXECUTABLE_PATH = "chromedriver"

9. It's coming out , Defined executable_path The interpreter did not find , Just use it chromedriver 了 , So it leads to an error , Let's confirm the definition of this parameter :

- executable_path - Deprecated: path to the executable. If the default is used it assumes the executable is in the $PATH

10. thus , There are two solutions :
1. Directly modifying DEFAULT_EXECUTABLE_PATH For your machine chromedriver The path of
ha-ha , But this method only applies to your machine , So let's consider script parameters executable_path:
Isn't that the first parameter ? Just pass it in , Be accomplished :

from selenium import webdriver
import time

browser = webdriver.Chrome("F:\google_download\chromedriver_99.0/chromedriver.exe")
browser.get('http://www.baidu.com/')
time.sleep(10)

版权声明
本文为[liulanba]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204232008077745.html