Python Web 自动化测试之异常cannot find Chrome binary

代码:

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

结果:

PS E:30.Study30.自动化测试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 FilesPython38libsite-packagesseleniumwebdriverchromewebdriver.py", line 76, in __init__
    RemoteWebDriver.__init__(
  File "C:Program FilesPython38libsite-packagesseleniumwebdriver emotewebdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:Program FilesPython38libsite-packagesseleniumwebdriver emotewebdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:Program FilesPython38libsite-packagesseleniumwebdriver emotewebdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:Program FilesPython38libsite-packagesseleniumwebdriver emoteerrorhandler.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.Study30.自动化测试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.Study30.自动化测试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 FilesPython38libsite-packagesseleniumwebdrivercommonservice.py", line 72, in start
            self.process = subprocess.Popen(cmd, env=self.env,
          File "C:Program FilesPython38libsubprocess.py", line 854, in __init__
            self._execute_child(args, executable, preexec_fn, close_fds,
          File "C:Program FilesPython38libsubprocess.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 FilesPython38libsite-packagesseleniumwebdriveredgewebdriver.py", line 56, in __init__
            self.edge_service.start()
          File "C:Program FilesPython38libsite-packagesseleniumwebdrivercommonservice.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:xxxchromedriver.exe')(来自1.)

    3.3.添加chrome.exe到系统path环境变量(来自1.)

    3.4.在代码中指定chrome.exe绝对路径。设置binary_location属性(来自1.)

        option = webdriver.ChromeOptions()
        option.binary_location=r'C:UsersAdministratorAppDataLocalGoogleChromeApplicationchrome.exe'
        driver = webdriver.Chrome() driver.get('https://www.baidu.com')
        注:解决方案1在遇到问题:selenium.common.exceptions.WebDriverException: Message: unknown error: call function result missing 'value'时,也可解决

原文地址:https://www.cnblogs.com/hadas/p/13149593.html