小编Nik*_*Con的帖子

消息:尝试在不建立连接的情况下运行命令

新来的,对新手问题表示歉意。

尝试使用 Python、Selenium 和 unittest 模块运行脚本。具有典型的setUp()、test_1、test_2、tearDown()方法结构。由于我添加了多个测试,因此出现以下错误:selenium.common.exceptions.InvalidSessionIdException:消息:尝试在没有建立连接的情况下运行命令

我该如何解决这个问题?

我研究过人们在这个问题上遇到的类似问题,但在几乎所有情况下,这个问题与我遇到的任何事情都没有关系(例如 cronjobs)

我的程序看起来像这样......

class MyTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        #my setup code here...
        cls.driver = webdriver.Firefox(executable_path='my_gecko_driver')
        cls.driver.get('www.my_url.com')
        cls.driver......  # various other tasks

    def test_1(self):
        # my test code here....
        foo = self.driver.find_element_by_xpath('/button_elem/')
        foo.click()
        # etc etc....

    def test_2(self):
        # my test code here....
        bar = self.driver.find_element_by_xpath('/button_elem/')
        bar.click()
        # etc etc....

    @classmethod
    def tearDown(cls):
        print('Entered tearDown function.')
        # close the browser window
        cls.driver.close()


if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)

在我添加第二个测试之前,测试运行成功。

现在我收到错误:selenium.common.exceptions.InvalidSessionIdException:消息:尝试在不建立连接的情况下运行命令

我怀疑这与tearDown 方法可能无法正常工作有关?但是我认为这个方法是在每个 test_x 完成后调用的。

我还注意到 …

selenium python-3.x geckodriver

6
推荐指数
1
解决办法
9657
查看次数

标签 统计

geckodriver ×1

python-3.x ×1

selenium ×1