硒未连接

2024-04-19 11:15:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我正试着做些测试,结果遇到了路障

这是脚本的开始,在这里它被捕获了。在

from selenium import selenium
import subprocess
import time
import sys
import socket
from os.path import dirname
import unittest
from pushdata import push


class selenium_tests(unittest.TestCase):

    @classmethod
    def setUpClass(self):
        directory = dirname(__file__)
        path = directory + '/selenium-server-standalone-2.28.0.jar'
        sub = subprocess.Popen('exec java -jar ' + path,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT,
                               shell=True)
        self.selenium_server = sub
        count = 0
        while True:  # ensure the server is established
            try:
                self.selenium = selenium("localhost",
                                         4444,
                                         "*chrome",
                                         "http://127.0.0.1:8000/resources/")
                self.selenium.start()
                break
            except socket.error, v:
                count += 1
                if count == 10:
                    message = "- Selenium server took to long to establish"
                    print "\n", v, message
                    sys.exit()
                time.sleep(1)

要运行测试,我使用以下命令:

^{pr2}$

这引发了:

[Errno 111] Connection refused - Selenium server took to long to establish

所以我知道建立联系有困难,只是不知道为什么?在

注意:当我使用django测试框架运行它时,它可以工作,但当我尝试手动运行它时就不行了

编辑:

当我在单独的shell中运行selenium服务器时,它可以工作

java -jar selenium-server-standalone-2.28.0.jar

所以现在我假设这可能是原因:

sub = subprocess.Popen('exec java -jar ' + path,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.STDOUT,
                               shell=True)

Tags: topathfromimportselftrueservertime