国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

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

這篇具有很好參考價值的文章主要介紹了selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

最近需要使用一下selenium,剛運行就報錯了。。。
前提準備:
1.安裝selenium
2.下載chrome對應版本的chromedriver
代碼就是一個簡單的demo:

from selenium import webdriver
import time

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

運行報錯:
selenium.common.exceptions.webdriverexception: message: 'chromedriver' execu,自動化測試,python
網(wǎng)上說要把chromedriver放到環(huán)境變量,放進去還是報錯?。?br> 然后就直接看源碼吧:
這個過程很繁瑣,很枯燥,嫌廢話連篇請直接翻到文末解決方法~~
這個過程很繁瑣,很枯燥,嫌廢話連篇請直接翻到文末解決方法~~
這個過程很繁瑣,很枯燥,嫌廢話連篇請直接翻到文末解決方法~~
1.代碼報錯入口:

browser = webdriver.Chrome()

2.首先看報錯被掛起的地方:

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)

對應代碼:

    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.是start()方法的try失敗了,這里可以看到是self.path的問題,再往上回溯看調用start方法的地方,self.path = executable找到傳值 executable的地方:

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

相關代碼如下:


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.粗看似乎沒什么問題,self.service.start()前面并沒有進行什么特殊定義和處理,并沒有進行傳參"self.path",這時候就要考慮python 的繼承問題了,我進入它的父類RemoteWebDriver也沒有發(fā)現(xiàn)導致該報錯的問題,這時就要考慮子類了,我們此時還忽略了一個問題:報錯入口: browser = webdriver.Chrome()

webdriver.Chrome()代碼如下:

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.這不就連起來了嗎,子類調用父類的__init__方法:

super(WebDriver, self).__init__ #和第3步相連

6.這個時候我們要關注Service類的傳參,即super(WebDriver, self).__init__的倒數(shù)第二個參數(shù)service:

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

對應代碼:

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")

這里有同學可能有疑問了?
此處的Service和第2步的Service不是一個類??!
是的,這里還有兩層嵌套關系,直接ctrl點擊進去就可以看:

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

7.根據(jù)第6步的關系我們知道:在第2步Service出現(xiàn)問題的self.path就是這里面的executable_path,我們來看下這個參數(shù)的定義:

executable_path=DEFAULT_EXECUTABLE_PATH

8.刨根究底來看這個常量的定義:

DEFAULT_EXECUTABLE_PATH = "chromedriver"

9.這不就出來了,定義的executable_path 解釋器沒有找到,就直接使用chromedriver了,所以導致報錯,我們確認一下這個參數(shù)的定義:

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

10.至此,有兩個解決辦法:
1.直接修改DEFAULT_EXECUTABLE_PATH 為你機器的chromedriver的路徑
哈哈,但是這個方法只適用于你的機器了,因此我們考慮腳本傳參executable_path:
那不就是第一個參數(shù)嗎?直接傳進去,大功告成:文章來源地址http://www.zghlxwxcb.cn/news/detail-808578.html

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)

到了這里,關于selenium.common.exceptions.WebDriverException: Message: ‘chromedriver‘ executable needs to be in PAT的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包