Python 警告(来自警告模块):资源警告:未关闭<;socket.socket对象,fd=404,family=2,type=1,proto=0>;使用硒
我收到了这个警告。怎么了Python 警告(来自警告模块):资源警告:未关闭<;socket.socket对象,fd=404,family=2,type=1,proto=0>;使用硒,python,sockets,selenium,Python,Sockets,Selenium,我收到了这个警告。怎么了 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
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()
警告(来自警告模块):
文件“C:\Python33\lib\site packages\selenium-2.37.2-py3.3.egg\selenium\webdriver\firefox\firefox\u binary.py”,第95行
虽然不是utils.is_可连接(self.profile.port):
资源警告:未关闭
这是一个已知的错误:
尽管如此,忽略它是安全的。如果您使用的是Python 3,则可以执行以下操作:
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 2中,您将使用如下内容:
unittest.main(warnings='ignore')
cf
如果你原谅我无耻的自我推销,我写的一本小册子中有更多关于硒的信息。我使用
-W
标志运行测试,因此:
with warnings.catch_warnings(record=True):
unittest.main()
或
这将抑制ResourceWarning
,但仍允许断言错误报告
顺便说一句,我发现:
python -W ignore -m unittest my_tests.MyIndividualTest
在每个测试都要运行时调用python my_tests.py
时工作,但此调用阻止运行单个测试
我不知道如何使用unittest.main(warnings='ignore')
而不会遇到错误,我认为这是因为递归包含了unittest
库
(
selenium==2.44.0
在python3.4.2
下)我们将在代码中的忽略警告语句放在哪里?它是否属于def setUp()
的一部分?这会抑制所有警告吗?是否可以仅抑制资源警告
?尽管这篇文章年代久远,而且链接到谷歌代码,但这个问题仍在发生。
python -W ignore -m unittest my_tests.MyIndividualTest
if __name__ == '__main__':
unittest.main(warnings='ignore')