Python - Webdriver命令不会触发

时间:2015-05-12 12:26:23

标签: python selenium

我创建了以下Python - 当我执行它时,我收到一个错误。明显的错误行以斜体显示

from selenium import webdriver
import unittest
import sys

class ExampleTestCase(unittest.TestCase):

    def setUp(self):
        * Errant line below
        self.__driver = webdriver.Remote(desired_capabilities={
            "browserName": "firefox",
            "platform": "Windows",*
        })
        print("Got this far")

    def test_example(self):

        self.__driver.get("http://www.google.com")
        self.__assertEqual(self.driver.title, "Google")

    def tearDown(self):
        self.__driver.quit()

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

错误是

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

During handling of the above exception, another exception occurred:

有谁可以提出问题可能是什么?我正在使用Python 3.4和

1 个答案:

答案 0 :(得分:1)

您的代码正在尝试调用Selenium的远程实例,只有在远程服务器能够响应您的请求时才能正常运行。如果您没有指定地址(您没有),那么它将尝试在本地计算机上进行连接。如果想要做的只是在本地计算机上启动Firefox,那么你可以这样做:

from selenium import webdriver
import unittest
import sys

class ExampleTestCase(unittest.TestCase):

    def setUp(self):
        self.__driver = webdriver.Firefox()
        print("Got this far")

    def test_example(self):

        self.__driver.get("http://www.google.com")
        self.assertEqual(self.__driver.title, "Google")

    def tearDown(self):
        self.__driver.quit()

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

上面的代码运行正常。