Skip to main content
 首页 » 编程设计

python之Django Jenkins 在处理到 Selenium 服务器时引发 WebDriverException

2024年08月29日39落叶

我通过命令启动Selenium server hub

java -jar selenium-server-standalone-2.33.0.jar -role hub 

通过命令和Selenium服务器节点

java -jar selenium-server-standalone-2.33.0.jar -role node -hub http://localhost:4444/grid/register -browser browserName=htmlunit 

然后我尝试执行代码:

from selenium import webdriver 
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 
server =  'http://localhost:4444/wd/hub' 
dc = DesiredCapabilities.HTMLUNIT 
browser = webdriver.Remote(server, dc) 
browser.get('http://localhost:8000') 

之后一切正常。 但是当我尝试开始 Jenkins 测试时:

from django.test import TestCase, LiveServerTestCase 
from selenium.webdriver.common import proxy 
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities 
from selenium.webdriver.remote.webdriver import WebDriver 
 
class SeleniumTest(LiveServerTestCase): 
 
    @classmethod 
    def setUpClass(cls): 
        p = proxy.Proxy({ 
        'proxyType': proxy.ProxyType().MANUAL, 
        'httpProxy': '127.0.0.1:4444', 
        }) 
 
        capabilities = DesiredCapabilities().HTMLUNIT 
        cls.selenium = WebDriver(desired_capabilities=capabilities, proxy=p) 
        super(SeleniumTest, cls).setUpClass() 
 
    @classmethod 
    def tearDownClass(cls): 
        cls.selenium.quit() 
        super(SeleniumTest, cls).tearDownClass() 
 
    def test_javascript_basket(self): 
        self.selenium.get('http://localhost:8000') 

我收到以下错误,包含在回溯中:

WebDriverException: Message: u'\n\n\nERROR: The requested URL could not be retrieved\n\n\n\n

ERROR

\n

The requested URL could not be retrieved

\n\n
\n\n\n

The following error was encountered while trying to retrieve the URL: a href="http://localhost:4444/wd/hub/session" localhost:4444/wd/hub/session ap\n\n\n

Connection to 127.0.0.1 failed.

\n
\n\n系统返回: (111) Connection refused

\n\n

远程主机或网络可能已关闭。请重试该请求。

\n\n

您的缓存管理员是网站管理员。

\n\n
\n\n\n
\n\n< p>由本地主机 (squid/3.1.6) 于 2013 年 6 月 10 日星期一 04:36:42 GMT 生成

\n\n\n'

这是怎么回事?为什么从 Jenkins 测试连接到 Selenium 服务器不起作用?

python==2.7.3 
Django==1.5 
django-jenkins==0.14.0 
selenium==2.33.0 

更新:如果我使用 Firefox WebDriver 而不是 HTMLUNIT,Firefox 会在行之后打开

cls.selenium = WebDriver(desired_capabilities=capabilities, proxy=p) 

,但稍后引发上述异常。

已解决 我只是添加到 setUpClass() 方法:

import os 
. . . 
    def setUpClass(cls): 
        os.environ['NO_PROXY'] = '127.0.0.1' 

请您参考如下方法:

我以这种方式解决了这个问题(使用了 phantom-js 而不是 HTMLUNIT,因为它是唯一剩下的稳定版本的代码)。

from django.test import LiveServerTestCase 
from selenium import webdriver 
from os import environ 
 
 
class SeleniumTestCase(LiveServerTestCase): 
    __test__ = False     
 
    @classmethod 
    def setUpClass(cls): 
        environ['NO_PROXY'] = '127.0.0.1'  # The key point 
 
        cls.selenium = webdriver.PhantomJS(service_args=['--proxy-type=none'])     
        super(SeleniumTestCase, cls).setUpClass() 
 
    @classmethod 
    def tearDownClass(cls): 
        cls.selenium.close() 
        cls.selenium.quit() 
        super(SeleniumTestCase, cls).tearDownClass() 
 
 
class TestFoo(SeleniumTestCase):     
    def setUp(self): 
        # do something before every test method runs 
        pass 
    def test_foo(self): 
        # test 
        pass