首页 > 解决方案 > selenium web 测试中的断言失败

问题描述

当我尝试执行一个新的单元测试用例时,它会显示以下错误。

注意:由于我已将 chrome Web 驱动程序放在 D 盘中,因此我无权设置路径。

测试开始于 14:56 ... C:\Users\xxx.xxxx\PycharmProjects\Automation\venv\Scripts\python.exe "C:\Users\xx.xxx\AppData\Local\JetBrains\PyCharm Community Edition 2019.2\ helpers\pycharm_jb_unittest_runner.py" --path C:/Users/xx.xxx/PycharmProjects/Automation/UnitTest_1.py 使用参数启动单元测试 python -m unittest C:/Users/xx.xx/PycharmProjects/Automation/UnitTest_1.py in C:\Users\xx.xxx\PycharmProjects\Automation

错误回溯(最后一次调用):文件“C:\xxxx\xxxxx\PycharmProjects\Automation\venv\lib\site-

packages\selenium\webdriver\common\service.py”,第 76 行,在 start stdin=PIPE 中)文件“C:\Programs\Python\Python37-32\lib\subprocess.py”,第 775 行,在init restore_signals,start_new_session ) 文件 "C:\Programs\Python\Python37-32\lib\subprocess.py",第 1178 行,在 _execute_child 启动信息中) FileNotFoundError: [WinError 2] 系统找不到指定的文件

当我处理上述异常时,我在下面得到了另一个异常。

回溯(最近一次调用最后):文件“C:\Programs\Python\Python37-32\lib\unittest\case.py”,第 59 行,在 testPartExecutor 产生文件“C:\Programs\Python\Python37-32\lib \unittest\case.py”,第 615 行,在运行 testMethod() 文件“C:\xxxx\xxxxx\PycharmProjects\Automation\UnitTest_1.py”,第 17 行,在 test_Bing self.driver = webdriver.Chrome(executable_path=" D:\test\chromedriver.exe") 文件“C:\xxxx\xxxx\PycharmProjects\Automation\venv\lib\site-packages\selenium\webdriver\chrome\webdriver.py”,第 73 行,在init self.service.start() 文件“C:\xxxx\xxxxx\PycharmProjects\Automation\venv\lib\site-packages\selenium\webdriver\common\service.py”,第 83 行,在 start os.path.basename( self.path), self.start_error_message) selenium.common.exceptions.WebDriverException:消息:“chromedriver.exe”可执行文件需要在 PATH 中。请参见

https://sites.google.com/a/chromium.org/chromedriver/home

在 2.374 秒内运行 2 次测试

FAILED (errors=2) 错误回溯(最近一次调用最后一次):文件“C:\Programs\Python\Python37-32\lib\unittest\case.py”,第 59 行,在 testPartExecutor 产生文件“C:\Programs\ Python\Python37-32\lib\unittest\case.py",第 615 行,运行 testMethod() 文件 "C:\Users\xxx.xxxx\PycharmProjects\Automation\UnitTest_1.py",第 10 行,在 test_Google self. driver.get("www.google.com") 文件“C:\Users\xx.xxxx\PycharmProjects\Automation\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py”,第 333 行,在获取 self.execute(Command.GET, {'url': url}) 文件“C:\Users\xx.xxxx\PycharmProjects\Automation\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py” ,第 321 行,在执行 self.error_handler.check_response(response) 文件“C:\Users\xx.xxxxx\PycharmProjects\Automation\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py",第 242 行,在 check_response 中引发 exception_class(message, screen, stacktrace) selenium.common.exceptions。 InvalidArgumentException:消息:无效参数(会话信息:chrome=77.0.3865.90)

断言失败

进程以退出代码 1 结束

断言失败

断言失败

import os
os.environ["PATH"] += os.pathsep +r"D:\test\chromedriver.exe"
import unittest
from selenium import webdriver

class SearchEnginesTest(unittest.TestCase):
    def test_Google(self):
        self.driver = webdriver.Chrome(executable_path=r"D:\test\chromedriver.exe")
        self.driver.get("www.google.com")
        self.driver.maximize_window()
        # self.driver.save_screenshot("D:\\test\\one.jpg")
        print("Title of the page :  " + self.driver.title)
        self.driver.close()

    def test_Bing(self):
        self.driver = webdriver.Chrome(executable_path="D:\test\chromedriver.exe")
        self.driver.get("www.bing.com")
        self.driver.maximize_window()
        # self.driver.save_screenshot("D:\\test\\two.jpg")
        print("Title of the page:    " + self.driver.title)
        self.driver.close()

if __name__ == "__main__":
    unittest.main()

标签: pythonselenium-webdriver

解决方案


检查代码中的所有路径是否可以访问 python 中的 os sys.path。此外,检查 *.py 脚本是否位于 python 路径列表文件“python._pth”中列出的目录中。检查有关路径更新的操作系统说明,在大多数“锁定”计算机中,您仍然可以编辑“用户路径”。另一种选择是创建一个“运行器”脚本 - Windows 上的 *.bat 或 shell 上的 *.sh 的 os 脚本。在运行脚本中设置/添加 os sys.path 并将文件动态添加到 python 路径,当脚本完成时,“添加”将消失回锁定路径。提交有关您的脚本的 ITIL 票证,以获得系统管理员的批准。


推荐阅读