Python selenium test gets stuck in urlopen Python selenium test gets stuck in urlopen selenium selenium

Python selenium test gets stuck in urlopen


Thanks to you all for pinpointing the problem, I took for inspiration this article http://nedbatchelder.com/blog/201103/quick_and_dirty_multithreaded_django_dev_server.html and modified my manage.py file:

#!/usr/bin/env pythonfrom http import serverimport osfrom socketserver import ThreadingMixInimport sysdef monkey_patch_test_server():    # This monkey-patches HTTPServer to create a base HTTPServer class that    # supports multithreading    originalhttpserver = server.HTTPServer    class ThreadedHTTPServer(ThreadingMixIn, originalhttpserver):        def __init__(self, server_address, RequestHandlerClass=None):            originalhttpserver.__init__(self, server_address,                                        RequestHandlerClass)    server.HTTPServer = ThreadedHTTPServerif __name__ == "__main__":    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "planexo.settings")    from django.core.management import execute_from_command_line    if sys.argv[1] == 'test':        monkey_patch_test_server()    execute_from_command_line(sys.argv)

Now it works!


Use Python faulthandler module to print the traceback of all hung threads. This will pinpoint you the actual functions were the hanging is happening.

If needed edit your question and add the relevant information.

Otherwise my guess is that you are using Django test / development server in such a way that it can only handle one request at a time. Your initial HTTP request to dev server triggers a request to request data from itself, which Django dev server is unable to do. But this is just a guess.

Also relevant source code missing from the question.


I'll share my case:

I had this issue with my Django selenium tests (django==1.7.12 and selenium==2.53.1) with ChromeDriver 2.21.371459 and Google Chrome 48.0.2564.116.

I was able to isolate the issue. In my case it was happening only for pages referencing a static file (an image in a HTML <img> tag for instance http://cdn.local.myproject.net/static/myimage.png) on my custom local cdn domain. The issue was not present if I used a relative path "/static/myimage.png" or localhost "http://127.0.0.1/static/myimage.png" so I figured it was a DNS problem.

I was able to bypass the problem by using the --dns-prefetch-disable option of chrome.

Example in Python:

from selenium.webdriver import Chromefrom selenium.webdriver.chrome.options import Optionschrome_options = Options()chrome_options.add_argument('--dns-prefetch-disable')driver = Chrome(chrome_options=options)

I don't know if this is the general case but hopefully it can help some of you.