关于google chrome:selenium.common.exceptions.WebDriverException:消息:通过Python unittest模块执行测试用例时没有这样的会话

selenium.common.exceptions.WebDriverException: Message: no such session while executing testcases through Python unittest module

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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("[email protected]")
        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("[email protected],[email protected]")
        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()

有人能帮我解决这个问题吗?

我正在尝试运行多个函数并获取错误:

正在查找文件…完成。正在导入测试模块…完成。

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

错误:测试主页(kenzen.kenlogin.kenlogin)

Traceback (most recent call last): File
"E:\Learning\Workplace\kenZen\kenZen\KenLogin.py", line 37, in
test_home
driver.implicitly_wait(2000) File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\webdriver.py",
line 895, in implicitly_wait
'ms': float(time_to_wait) * 1000}) File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\webdriver.py",
line 314, in execute
self.error_handler.check_response(response) File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: no such
session (Driver info: chromedriver=2.38.552522
(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT
10.0.14393 x86_64)

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

错误:测试主页(kenzen.kenlogin.kenlogin)

Traceback (most recent call last): File
"E:\Learning\Workplace\kenZen\kenZen\KenLogin.py", line 60, in
tearDown
driver.close() File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\webdriver.py",
line 681, in close
self.execute(Command.CLOSE) File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\webdriver.py",
line 314, in execute
self.error_handler.check_response(response) File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: no such
session (Driver info: chromedriver=2.38.552522
(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT
10.0.14393 x86_64)

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

错误:测试登录名(kenzen.kenlogin.kenlogin)

Traceback (most recent call last): File
"E:\Learning\Workplace\kenZen\kenZen\KenLogin.py", line 21, in
test_login
driver.implicitly_wait(2000) File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\webdriver.py",
line 895, in implicitly_wait
'ms': float(time_to_wait) * 1000}) File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\webdriver.py",
line 314, in execute
self.error_handler.check_response(response) File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: no such
session (Driver info: chromedriver=2.38.552522
(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT
10.0.14393 x86_64)

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

错误:测试登录名(kenzen.kenlogin.kenlogin)

Traceback (most recent call last): File
"E:\Learning\Workplace\kenZen\kenZen\KenLogin.py", line 60, in
tearDown
driver.close() File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\webdriver.py",
line 681, in close
self.execute(Command.CLOSE) File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\webdriver.py",
line 314, in execute
self.error_handler.check_response(response) File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: no such
session (Driver info: chromedriver=2.38.552522
(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT
10.0.14393 x86_64)

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

错误:test_newteam(kenzen.kenlogin.kenlogin)

Traceback (most recent call last): File
"E:\Learning\Workplace\kenZen\kenZen\KenLogin.py", line 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")
File
"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\webdriver.py",
line 387, in 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
emote\webdriver.py",
line 957, in find_element
'value': value})['value'] File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\webdriver.py",
line 314, in execute
self.error_handler.check_response(response) File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: no such
session (Driver info: chromedriver=2.38.552522
(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT
10.0.14393 x86_64)

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

错误:test_newteam(kenzen.kenlogin.kenlogin)

Traceback (most recent call last): File
"E:\Learning\Workplace\kenZen\kenZen\KenLogin.py", line 60, in
tearDown
driver.close() File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\webdriver.py",
line 681, in close
self.execute(Command.CLOSE) File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\webdriver.py",
line 314, in execute
self.error_handler.check_response(response) File"C:\Users\Pardeep\AppData\Local\Programs\Python\Python36-32\lib\site-packages\selenium\webdriver
emote\errorhandler.py",
line 242, in check_response
raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: no such
session (Driver info: chromedriver=2.38.552522
(437e6fbedfa8762dec75e2c5b3ddb86763dc9dcb),platform=Windows NT
10.0.14393 x86_64)

1
2
3
4
----------------------------------------------------------------------
Ran 4 tests in 7.928s

FAILED (errors=6)

在使用python和unittest时,需要注意以下事项:

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    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("[email protected]")
            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()
  • 控制台输出:

    1
    2
    3
    4
    5
    6
    Login Pass
    .
    ----------------------------------------------------------------------
    Ran 1 test in 13.731s

    OK

如果您希望只调用一次setup()函数,那么根据您的注释更新,您可以使用setUpClasstearDownClass类方法,这些方法在类执行时在测试前后执行。

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

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    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("[email protected]")
            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()
  • 控制台输出:

    1
    2
    3
    4
    5
    6
    7
    ENTER Key pressed
    .Login Pass
    .
    ----------------------------------------------------------------------
    Ran 2 tests in 18.187s

    OK

工具书类

  • 在这里,您可以找到有关SeleniumPython中什么是UnitTest的详细讨论。

  • 在这里,您可以找到有关几个测试的UnitTest设置/拆卸的详细讨论。

  • 在这里,您可以只找到一次有关运行设置的详细讨论