Python Web 自動化測試之異常cannot find Chrome binary


代碼:

#coding=utf-8
from selenium import webdriver
webdriver.Chrome()

結果:

PS E:\30.Study\30.自動化測試\99.零基礎入門 Python Web 自動化測試\10.seleniumCodePractice> & "C:/Program Files/Python38/python.exe" "e:/30.Study/30.自動化測
試/99.零基礎入門 Python Web 自動化測試/10.seleniumCodePractice/open_browser.py"
Traceback (most recent call last):
  File "e:/30.Study/30.自動化測試/99.零基礎入門 Python Web 自動化測試/10.seleniumCodePractice/open_browser.py", line 3, in <module>
    webdriver.Chrome()
  File "C:\Program Files\Python38\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "C:\Program Files\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Program Files\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Program Files\Python38\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Program Files\Python38\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
  (Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 10.0.18363 x86_64)

PS E:\30.Study\30.自動化測試\99.零基礎入門 Python Web 自動化測試\10.seleniumCodePractice>

原因:

cannot find Chrome binary”是“找不到Chrome二進制文件”的意思(也就是找不到Chrome瀏覽器),而尋找的key就是Chrome的版本號2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a)。由此可知,selenium對於瀏覽器及其瀏覽器驅動是要求匹配的。這里因為使用的是綠色版Chrom瀏覽器,驅動也是隨便下載的,所以報了這個異常。

接下來把selenium調取的瀏覽器換為Edge瀏覽器,確認了一下版本號(版本 83.0.478.45 (官方內部版本) (64 位)),從下記下載了匹配的驅動(*1),解壓后將“msedgedriver.exe”-->"MicrosoftWebDriver.exe"后(*2),置於pyhon安裝根目錄,執行webdriver.Edge()即可。

*1.Edge瀏覽器驅動:https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/

*2.更改驅動名字原因是因為selenium內調取的Edge驅動名字是“MicrosoftWebDriver.exe”,如果不改的話,會出現“FileNotFoundError: [WinError 2] 系統找不到指定的文件。”異常,找不到的原因就是名字不一樣。由此可知,selenium對於瀏覽器驅動的名字是區分大小寫且要與內置的名字必須吻合才可以。

    *2.1.MicrosoftWebDriver.exe:

       

     *2.2.“FileNotFoundError: [WinError 2] 系統找不到指定的文件。”異常日志:

        PS E:\30.Study\30.自動化測試\99.零基礎入門 Python Web 自動化測試\10.seleniumCodePractice> & "C:/Program Files/Python38/python.exe" "e:/30.Study/30.自動化測
        試/99.零基礎入門 Python Web 自動化測試/10.seleniumCodePractice/open_browser.py"
        Traceback (most recent call last):
          File "C:\Program Files\Python38\lib\site-packages\selenium\webdriver\common\service.py", line 72, in start
            self.process = subprocess.Popen(cmd, env=self.env,
          File "C:\Program Files\Python38\lib\subprocess.py", line 854, in __init__
            self._execute_child(args, executable, preexec_fn, close_fds,
          File "C:\Program Files\Python38\lib\subprocess.py", line 1307, in _execute_child
            hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
        FileNotFoundError: [WinError 2] 系統找不到指定的文件。

        During handling of the above exception, another exception occurred:

        Traceback (most recent call last):
          File "e:/30.Study/30.自動化測試/99.零基礎入門 Python Web 自動化測試/10.seleniumCodePractice/open_browser.py", line 5, in <module>
            webdriver.Edge()
          File "C:\Program Files\Python38\lib\site-packages\selenium\webdriver\edge\webdriver.py", line 56, in __init__
            self.edge_service.start()
          File "C:\Program Files\Python38\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
            raise WebDriverException(
        selenium.common.exceptions.WebDriverException: Message: 'MicrosoftWebDriver.exe' executable needs to be in PATH. Please download from http://go.microsoft.com/fwlink/?LinkId=619687

 

總結:

 1.參照原文鏈接:https://blog.csdn.net/n123456uo/java/article/details/91420342

 2.參照原文鏈接:https://blog.csdn.net/yangyifan_0811/java/article/details/86630290

 3.根據上記的1和2,可以了解cannot find Chrome binary的發生可能用以下幾個方法解決:

    3.1.selenium對於瀏覽器版本號及其版本號所對應的瀏覽器驅動是要求匹配的(來自2.)

    3.2.指定chromedriver.exe驅動絕對路徑:driver = webdriver.Chrome(r'd:\xxx\chromedriver.exe')(來自1.)

    3.3.添加chrome.exe到系統path環境變量(來自1.)

    3.4.在代碼中指定chrome.exe絕對路徑。設置binary_location屬性(來自1.)

        option = webdriver.ChromeOptions()
        option.binary_location=r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe'
        driver = webdriver.Chrome() driver.get('https://www.baidu.com')
        注:解決方案1在遇到問題:selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missing 'value'時,也可解決

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM