OSError: [WinError 6] The handle is invalid [Selenium Python]

╄→尐↘猪︶ㄣ 提交于 2021-02-07 09:26:26

问题


I've been running my suite of tests and I'm seeing this error message when I execute my suite.

Command used in cmd line:

py TestSuite.py

Output:

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

The two Tracebacks repeat for all 5 tests that has ERROR. So to debug this, I used the following code to comment out all except for one of the tests that and an error, say Test3.

@unittest.skip("demonstrating skipping")

The result is the following:

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'

I did this for every test that had the error status. So I would skip all other tests and run only that test. The result is the same as before, that specific test passes as OK with every other test skipped.

I did make sure that chromedriver executable is in my PATH. I'm using Windows 7. I can check by typing the following into my command line:

chromedriver -v

Output:

ChromeDriver 2.12.301325 (*some random numbers goes here*)

Python version: 3.4.3. Python bindings for Selenium: 2.46.0

So my question is, why am I seeing the ERROR in 5 of my tests but not in the first 2? And why do I not see any if I only run 1 test at a time and skip the rest?

UPDATE

I tried using Firefox driver and that works perfectly fine executing all of my tests. The only bad thing is that if I have any changes on my page, Firefox driver will launch a popup asking me if I'm positive that I want to leave the page. Chromedriver doesn't do that when self.driver.close() gets called.

As requested, here is the code snippet to help recreate the issue:

# 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()

来源:https://stackoverflow.com/questions/31079460/oserror-winerror-6-the-handle-is-invalid-selenium-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!