【问题标题】:Warning (from warnings module): ResourceWarning: unclosed <socket.socket object, fd=404, family=2, type=1, proto=0> using selenium警告(来自警告模块):ResourceWarning: unclosed <socket.socket object, fd=404, family=2, type=1, proto=0> using selenium
【发布时间】:2014-01-02 14:54:02
【问题描述】:
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


class PythonOrgSearch(unittest.TestCase):

    def setUp(self):
        self.driver = webdriver.Firefox()

    def test_search_in_python_org(self):
        driver = self.driver
        driver.get("http://www.python.org")
        self.assertIn("Python", driver.title)
        elem = driver.find_element_by_name("q")
        elem.send_keys("selenium")
        elem.send_keys(Keys.RETURN)
        self.assertIn("Google", driver.title)

    def tearDown(self):
        self.driver.close()

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

我收到此警告。怎么了?

Warning (from warnings module):
  File "C:\Python33\lib\site-packages\selenium-2.37.2-py3.3.egg\selenium\webdriver\firefox\firefox_binary.py", line 95
    while not utils.is_connectable(self.profile.port):
ResourceWarning: unclosed <socket.socket object, fd=400, family=2, type=1, proto=0>

【问题讨论】:

    标签: python sockets selenium


    【解决方案1】:

    因此,我使用-W 标志运行我的测试:

    python -W ignore -m unittest my_tests  
    

    python -W ignore -m unittest my_tests.MyIndividualTest     
    

    抑制ResourceWarning,但仍允许断言错误报告。


    顺便说一句,我发现:

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

    在要运行每个测试时调用 python my_tests.py 时有效,但此调用会阻止运行单个测试。

    我无法弄清楚如何在不遇到错误的情况下使用unittest.main(warnings='ignore'),我将其归结为递归包含unittest 库。

    (selenium==2.44.0Python 3.4.2)

    【讨论】:

      【解决方案2】:

      这是一个已知的错误:

      http://code.google.com/p/selenium/issues/detail?id=5923

      尽管忽略它是安全的。如果你使用的是 Python 3,你可以这样做:

      unittest.main(warnings='ignore')
      

      Python 3 unittest docs

      在 Python 2 中,你会使用这样的东西:

      with warnings.catch_warnings(record=True):
           unittest.main()
      

      参见Python 2 warnings docs

      如果你能原谅这种无耻的自我推销,我写的一本小书里有更多关于硒的信息,here

      【讨论】:

      • 我们应该将忽略警告语句放在代码中的什么位置?它会是def setUp() 的一部分吗?
      • 这会抑制所有警告吗?是否可以只抑制ResourceWarning?尽管这篇文章和谷歌代码的链接已经过时了,但这个问题仍然存在。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-25
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 2016-07-05
      • 2016-02-22
      • 1970-01-01
      相关资源
      最近更新 更多