首页 > 解决方案 > 连接中止问题 - Selenium

问题描述

我正在阅读 Selenium/Python 书籍,当我按原样运行以下代码时,它运行良好并返回单个测试的结果。但是,当我取消注释第二个测试(在下面注释掉)时,它返回一个错误:

ConnectionAbortedError: [WinError 10053] An established connection was aborted by the software in your host machine

我无法弄清楚这个问题,代码看起来与书中的相同。有任何想法吗?

from selenium import webdriver
import unittest


class SearchTests(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # create a new Chrome session
        cls.driver = webdriver.Chrome()
        cls.driver.implicitly_wait(30)
        cls.driver.maximize_window()

        # navigate to the application home page
        cls.driver.get('http://demo-store.seleniumacademy.com/')
        cls.driver.title

    def test_search_by_category(self):

        # get the search textbox
        self.search_field = self.driver.find_element_by_name("q")
        self.search_field.clear()

        # enter search keyword and submit
        self.search_field.send_keys('phones')
        self.search_field.submit()

        # get all the anchor elements which have product names displayed
        # currently on result page using find_elements_by_xpath method
        products = self.driver.find_elements_by_xpath("//h2[@class='product-name']/a")
        self.assertEqual(3, len(products))

    # def test_search_by_name(self):
    #     # get the search textbox
    #     self.search_field = self.driver.find_element_by_name("q")
    #     self.search_field.clear()
    #
    #     # enter search keyword and submit
    #     self.search_field.send_keys('salt shaker')
    #     self.search_field.submit()
    #
    #     # get all the anchor elements which have product names displayed
    #     # currently on result page using find_elements_by_xpath method
    #     products = self.driver.find_elements_by_xpath("//h2[@class='product-name']/a")
    #     self.assertEqual(1, len(products))

    @classmethod
    def tearDown(cls):

        # close the browser window
        cls.driver.quit()


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

编辑:我已经确认,当我注释掉第一个测试并只运行第二个测试时,它也运行良好。所以这是关于让两个测试都运行而引发错误的事情......

标签: pythonselenium

解决方案


推荐阅读