python-3.x - 消息:尝试在未建立连接的情况下运行命令
问题描述
对此新手,为新手问题道歉。
尝试使用 Python、Selenium 和 unittest 模块运行脚本。具有典型的 setUp()、test_1、test_2、tearDown() 方法结构。由于我添加了多个测试,因此出现以下错误:selenium.common.exceptions.InvalidSessionIdException: Message: 试图在未建立连接的情况下运行命令
我该如何解决这个问题?
我调查了人们在这个问题上遇到的类似问题,但在几乎所有情况下,这个问题都与我遇到的任何事情无关(例如 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()
在我添加第二个测试之前,测试运行成功。
现在我收到错误:selenium.common.exceptions.InvalidSessionIdException:消息:尝试在未建立连接的情况下运行命令
我怀疑这与 tearDown 方法可能无法正常工作有关?但是我认为这个方法是在每个 test_x 结束时调用的。
我还注意到 Pycharm 在“cls.driver.close()”行中突出显示“驱动程序”,我也不太确定。它说未解析的属性引用'但是这不是在 setUp() 方法中创建的吗?
解决方案
这是因为在您的机器上打开了多个浏览器会话。如果你在 linux 上运行命令
killall firefox
并尝试再次运行您的脚本。这应该为您解决错误。
推荐阅读
- laravel - 如何在 Vue FullCalendar 上的事件中添加图像?imageurl 返回未定义
- azure - 如何针对存储帐户运行多个子 Runbook
- c# - AmazonS3Client 找不到路径的一部分
- c# - 从 DataRow 第 N 列分配 DataGridRow.Header 值
- python - FreqDist 用于最常见的单词或短语
- angular - 如何将html值传递给javascript或通过环回使用两种方式绑定?
- java - 如何正确创建一组预定义的 JObject?
- r - 如何在 ggplot2 中按 geom / layer 进行分面?
- kubernetes - 构建/编译 Kubernetes 仅用于签名验证而不是签名
- android - Android Picasso Recyclerview 图片下载慢