OSError:[WinError 6]句柄无效[Selenium Python]

时间:2015-06-26 18:04:13

标签: python python-3.x selenium selenium-webdriver selenium-chromedriver

我一直在运行我的测试套件,当我执行套件时,我看到了这条错误消息。

cmd行中使用的命令:

py TestSuite.py

输出:

Test1 (__main__.TestSuite) ... ok
Test2 (__main__.TestSuite) ... ok
Test3 (__main__.TestSuite) ... ERROR
Test4 (__main__.TestSuite) ... ERROR
Test5 (__main__.TestSuite) ... ERROR
Test6 (__main__.TestSuite) ... ERROR
Test7 (__main__.TestSuite) ... ERROR

Traceback (most recent call last):
  File "E:\Python\lib\site-packages\selenium\webdriver\chrome\service.py", line
63, in start
    self.service_args, env=env, stdout=PIPE, stderr=PIPE)
  File "E:\Python\lib\subprocess.py", line 754, in __init__
    _cleanup()
  File "E:\Python\lib\subprocess.py", line 474, in _cleanup
    res = inst._internal_poll(_deadstate=sys.maxsize)
  File "E:\Python\lib\subprocess.py", line 1147, in _internal_poll
    if _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0:
OSError: [WinError 6] The handle is invalid

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "FacilitySettingsConfigIdentifiers.py", line 57, in setUp
    self.driver = webdriver.Chrome(chrome_options=options)
  File "E:\Python\lib\site-packages\selenium\webdriver\chrome\webdriver.py", lin
e 59, in __init__
    self.service.start()
  File "E:\Python\lib\site-packages\selenium\webdriver\chrome\service.py", line
70, in start
    http://code.google.com/p/selenium/wiki/ChromeDriver")
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executabl
e needs to be                 available in the path. Please look at
    http://docs.seleniumhq.org/download/#thirdPartyDrivers                 and r
ead up at                 http://code.google.com/p/selenium/wiki/ChromeDriver

对于具有ERROR的所有5个测试,两个回溯重复。因此,为了调试这个,我使用以下代码注释掉除了测试之外的所有测试和错误,比如Test3。

@unittest.skip("demonstrating skipping")

结果如下:

Test1 (__main__.TestSuite) ... skipped 'demonstrating skipping'
Test2 (__main__.TestSuite) ... skipped 'demonstrating skipping'
Test3 (__main__.TestSuite) ... ok
Test4 (__main__.TestSuite) ... skipped 'demonstrating skipping'
Test5 (__main__.TestSuite) ... skipped 'demonstrating skipping'
Test6 (__main__.TestSuite) ... skipped 'demonstrating skipping'
Test7 (__main__.TestSuite) ... skipped 'demonstrating skipping'

我为每个有错误状态的测试都这样做了。所以我会跳过所有其他测试并仅运行该测试。结果与之前相同,特定测试在跳过其他每个测试时都正常。

我确实确保chromedriver可执行文件在我的PATH中。我使用的是Windows 7.我可以通过在命令行中键入以下内容来检查:

chromedriver -v

输出:

ChromeDriver 2.12.301325 (*some random numbers goes here*)

Python版本:3.4.3。 Selenium的Python绑定:2.46.0

所以我的问题是,为什么我在5次测试中看到了ERROR而在前2次没有?如果我一次只运行1次测试并跳过剩下的部分,为什么我看不到任何问题?

更新

我尝试使用Firefox驱动程序,并且可以很好地执行我的所有测试。唯一不好的是,如果我的页面有任何更改,Firefox驱动程序将弹出一个弹出窗口,询问我是否肯定我要离开页面。当self.driver.close()被调用时,Chromedriver不会这样做。

根据要求,以下是帮助重新创建问题的代码段:

# Standard Imports
import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

class SimpleSearchTestsInChrome(unittest.TestCase):

    # setUp runs before each test
    def setUp(self):

        # Picks chrome driver to use and will launch chrome with a maximized screen
        options = webdriver.ChromeOptions()
        options.add_argument("--start-maximized")
        self.driver = webdriver.Chrome(chrome_options=options)
        #self.driver = webdriver.Firefox()

    def test_search_in_python_org_one(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("pycon")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source

    def test_search_in_python_org_two(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("pycon")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source

    def test_search_in_python_org_three(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("pycon")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source

    def test_search_in_python_org_four(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("pycon")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source

    def test_search_in_python_org_five(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("pycon")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source

    def test_search_in_python_org_six(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("pycon")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source        

    def test_search_in_python_org_seven(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("pycon")
        elem.send_keys(Keys.RETURN)
        assert "No results found." not in driver.page_source            

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

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

0 个答案:

没有答案