selenium.common.exceptions.WebDriverException:消息:通过Python unittest模块

时间:2018-05-28 06:16:23

标签: python google-chrome selenium-webdriver selenium-chromedriver python-unittest

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class KenLogin(unittest.TestCase):

    def test_globe(self):

        global driver

        driver = webdriver.Chrome(executable_path='E:\Learning\chromedriver.exe')
        driver.maximize_window()

    def test_login(self):



        driver.implicitly_wait(2000)
        driver.get("http://qa.kenzen.com")
        username = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/div[1]/input")
        password = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/div[2]/input")

        username.send_keys("pardeepk@clovity.com")
        password.send_keys("Kenzen@123")

        button = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/button")

        button.send_keys(Keys.ENTER)
        print("Login Pass")


    def test_home(self):

        driver.implicitly_wait(2000)

        teamsummary = driver.find_element_by_id("teamsummary")

        teamsummary.click()
        print("Home Pass")

    def test_newteam(self):

        addteam = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div/div[1]/ul[2]/li/button")

        addteam.click()

        teamname = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div/div[2]/div/div[1]/input")#.send_keys(data)

        admin = driver.find_element_by_id("administrator")
        admin.send_keys("qa@kenzen.com,qa1@kenzen.com")
        addteambutton = driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div/div[2]/div/div[6]/button")
        addteambutton.click()

        print("Team Added")

    def tearDown(self):
        driver.close()


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

任何人都可以帮我解决这个问题吗?

我正在尝试运行多个函数并收到错误:

查找文件...已完成。 导入测试模块......完成。

=============================================== =======================

错误:test_home(kenZen.KenLogin.KenLogin)

  

Traceback(最近一次调用最后一次):文件   " E:\ Learning \ Workplace \ kenZen \ kenZen \ KenLogin.py",第37行,在   test_home       driver.implicitly_wait(2000)File" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py",   第895行,在implicitly_wait中       ' ms':float(time_to_wait)* 1000})文件" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \远程\ webdriver.py&#34 ;,   第314行,执行中       self.error_handler.check_response(response)File" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py" ,   第242行,在check_response中       提出exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.WebDriverException:消息:没有这样的   session(驱动程序信息:chromedriver = 2.38.552522   (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform = Windows NT   10.0.14393 x86_64)

=============================================== =======================

错误:test_home(kenZen.KenLogin.KenLogin)

  

Traceback(最近一次调用最后一次):文件   " E:\ Learning \ Workplace \ kenZen \ kenZen \ KenLogin.py",第60行,在   拆除       driver.close()文件" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py",   681行,收盘       self.execute(Command.CLOSE)文件" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py" ,   第314行,执行中       self.error_handler.check_response(response)File" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py" ,   第242行,在check_response中       提出exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.WebDriverException:消息:没有这样的   session(驱动程序信息:chromedriver = 2.38.552522   (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform = Windows NT   10.0.14393 x86_64)

=============================================== =======================

错误:test_login(kenZen.KenLogin.KenLogin)

  

Traceback(最近一次调用最后一次):文件   " E:\ Learning \ Workplace \ kenZen \ kenZen \ KenLogin.py",第21行,在   test_login       driver.implicitly_wait(2000)File" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py",   第895行,在implicitly_wait中       ' ms':float(time_to_wait)* 1000})文件" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \远程\ webdriver.py&#34 ;,   第314行,执行中       self.error_handler.check_response(response)File" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py" ,   第242行,在check_response中       提出exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.WebDriverException:消息:没有这样的   session(驱动程序信息:chromedriver = 2.38.552522   (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform = Windows NT   10.0.14393 x86_64)

=============================================== =======================

错误:test_login(kenZen.KenLogin.KenLogin)

  

Traceback(最近一次调用最后一次):文件   " E:\ Learning \ Workplace \ kenZen \ kenZen \ KenLogin.py",第60行,在   拆除       driver.close()文件" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py",   681行,收盘       self.execute(Command.CLOSE)文件" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py" ,   第314行,执行中       self.error_handler.check_response(response)File" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py" ,   第242行,在check_response中       提出exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.WebDriverException:消息:没有这样的   session(驱动程序信息:chromedriver = 2.38.552522   (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform = Windows NT   10.0.14393 x86_64)

=============================================== =======================

错误:test_newteam(kenZen.KenLogin.KenLogin)

  

Traceback(最近一次调用最后一次):文件   " E:\ Learning \ Workplace \ kenZen \ kenZen \ KenLogin.py",第46行,in   test_newteam       addteam = driver.find_element_by_xpath(" / html / body / div [1] / div / div [2] / div / div / div [1] / ul [2] / li / button")   文件   " C:\用户\ Pardeep \应用程序数据\本地\程序\的Python \ Python36-32 \ lib中\站点包\硒\的webdriver \远程\ webdriver.py&#34 ;,   第387行,在find_element_by_xpath中       return self.find_element(by = By.XPATH,value = xpath)File" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py&#34 ;,   第957行,在find_element中       ' value':value})[' value']文件" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-包\硒\的webdriver \远程\ webdriver.py&#34 ;,   第314行,执行中       self.error_handler.check_response(response)File" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py" ,   第242行,在check_response中       提出exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.WebDriverException:消息:没有这样的   session(驱动程序信息:chromedriver = 2.38.552522   (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform = Windows NT   10.0.14393 x86_64)

=============================================== =======================

错误:test_newteam(kenZen.KenLogin.KenLogin)

  

Traceback(最近一次调用最后一次):文件   " E:\ Learning \ Workplace \ kenZen \ kenZen \ KenLogin.py",第60行,在   拆除       driver.close()文件" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py",   681行,收盘       self.execute(Command.CLOSE)文件" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ webdriver.py" ,   第314行,执行中       self.error_handler.check_response(response)File" C:\ Users \ Pardeep \ AppData \ Local \ Programs \ Python \ Python36-32 \ lib \ site-packages \ selenium \ webdriver \ remote \ errorhandler.py" ,   第242行,在check_response中       提出exception_class(消息,屏幕,堆栈跟踪)selenium.common.exceptions.WebDriverException:消息:没有这样的   session(驱动程序信息:chromedriver = 2.38.552522   (437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform = Windows NT   10.0.14393 x86_64)

----------------------------------------------------------------------
Ran 4 tests in 7.928s

FAILED (errors=6)

1 个答案:

答案 0 :(得分:1)

在使用Pythonunittest时,您需要注意以下事项:

  • def setUp(self) setUp()是初始化的一部分,在您要编写的每个测试函数之前都会调用此方法testcase类。
  • maximize_window() :而不是使用maximize_window()使用 chrome.options 类来最大化浏览器窗口。
  • implicitly_wait() Implicit Wait设置后,隐式等待将在 WebDriver 对象的生命周期内设置实例
  • 始终在driver.quit()方法中调用tearDown(){}以关闭&正常销毁 WebDriver Web客户端实例。
  • 这是您自己的代码块,上面提到了一些小调整:

    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    class KenLogin(unittest.TestCase):
    
        def setUp(self):
            global driver
            options = Options()
            options.add_argument("start-maximized")
            options.add_argument("disable-infobars")
            options.add_argument("--disable-extensions")
            cap = DesiredCapabilities().CHROME
            cap = options.to_capabilities()
            self.driver = webdriver.Chrome(desired_capabilities=cap, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    
        def test_login(self):
            self.driver.implicitly_wait(2000)
            self.driver.get("http://qa.kenzen.com")
            username = self.driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/div[1]/input")
            password = self.driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/div[2]/input")
            username.send_keys("pardeepk@clovity.com")
            password.send_keys("Kenzen@123")
            button = self.driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/button")
            button.send_keys(Keys.ENTER)
            print("Login Pass")
    
        def tearDown(self):
            self.driver.quit()
    
    
    if __name__ == "__main__":
        unittest.main()
    
  • 控制台输出:

    Login Pass
    .
    ----------------------------------------------------------------------
    Ran 1 test in 13.731s
    
    OK
    

根据您的评论更新,如果您只希望调用一次setup()函数,则可以使用setUpClasstearDownClass类方法,这些方法在测试之前和之后执行类被执行。

  • 这是您自己的代码块,上面提到了一些小调整:

    import unittest
    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    
    class KenLogin(unittest.TestCase):
    
        @classmethod
        def setUpClass(self):
            global driver
            options = Options()
            options.add_argument("start-maximized")
            options.add_argument("disable-infobars")
            options.add_argument("--disable-extensions")
            cap = DesiredCapabilities().CHROME
            cap = options.to_capabilities()
            self.driver = webdriver.Chrome(desired_capabilities=cap, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    
        def test_login(self):
            self.driver.implicitly_wait(2000)
            self.driver.get("http://qa.kenzen.com")
            username = self.driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/div[1]/input")
            password = self.driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/div[2]/input")
            username.send_keys("pardeepk@clovity.com")
            password.send_keys("Kenzen@123")
            button = self.driver.find_element_by_xpath("/html/body/div[1]/div/div[2]/div/div[2]/form/button")
            button.send_keys(Keys.ENTER)
            print("ENTER Key pressed")
    
        def test_print_success(self):
            print("Login Pass")
    
        @classmethod
        def tearDownClass(self):
            self.driver.quit()
    
    
    if __name__ == "__main__":
        unittest.main()
    
  • 控制台输出:

    ENTER Key pressed
    .Login Pass
    .
    ----------------------------------------------------------------------
    Ran 2 tests in 18.187s
    
    OK
    

参考