从Djang运行Selenium超时错误

2024-09-30 20:19:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Django运行SeleniumRC时遇到问题。我可以在pythonshell中运行Selenium docspython selenium client docs提供的示例代码,而不运行Django(所以不需要管理.py),但当我实际尝试从django测试用例或django shell运行Selenium时,我得到一个超时错误。 下面是我要运行的代码:


from selenium import selenium
from django.test import TestCase

class TestSelenium(TestCase):
    def setUp(self):
        self.verificationErrors = []
        self.selenium = selenium("localhost", 4444, "*firefox", "http://127.0.0.1:8000/")
        self.selenium.start()

    def test_foo(self):
        sel = self.selenium
        sel.open("/")

与管理.py试验注册.TestSelenium产生以下错误:

^{pr2}$

奇怪的是,尽管抛出了错误,Python停止了,SeleniumRC服务器确实启动了Firefox,但是由于Django已经停止,所以我无法运行更多的Selenium命令。这是SeleniumServer的输出:


14:21:48.362 INFO - Checking Resource aliases
14:21:48.369 INFO - Command request: getNewBrowserSession[*firefox, http://127.0.0.1:8000/, ] on session null
14:21:48.372 INFO - creating new remote session
14:21:48.443 INFO - Allocated session a3ea05a3d0eb4956ba69a67583ea49ba for http://127.0.0.1:8000/, launching...
14:21:48.533 INFO - Preparing Firefox profile...
14:21:51.473 INFO - Launching Firefox...
14:21:55.904 INFO - Got result: OK,a3ea05a3d0eb4956ba69a67583ea49ba on session a3ea05a3d0eb4956ba69a67583ea49ba

有人有什么想法吗?在


Tags: django代码pyselfinfohttpdocssession
2条回答

这将使用以浮点秒为单位的超时值来完成此操作:

import socket

from selenium import selenium
from django.test import TestCase

class TestSelenium(TestCase):
    def setUp(self):
        socket.settimeout(30)
        # ...
        self.selenium.start()

参考python stdlib docs

如果其他人有这个问题,我可以通过增加setUp方法中的套接字超时来解决它。在

相关问题 更多 >