Selenium远程webdriver错误(连接被拒绝)

时间:2018-06-06 21:21:47

标签: python python-3.x macos selenium

from selenium import webdriver
import sys
import os
cwd = os.getcwd()
driver= webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.CHROME,command_executor='http://127.0.0.1:4444')
driver.get('http://www.google.com')

错误:

urllib.error.URLError: <urlopen error [Errno 61] Connection refused>

这个程序不久前工作了,但现在却没有。这就是为什么我认为这是一个系统错误。 (OSX 10.13.4)

2 个答案:

答案 0 :(得分:1)

确保您已启动Selenium Standalone Server

java -jar /path/to/selenium-server-standalone-<version>.jar 

你应该看到类似这样的输出:

Jun 06, 2018 6:47:59 PM org.openqa.grid.selenium.GridLauncher main
INFO: Launching a standalone server
18:47:59.720 INFO - Java: Oracle Corporation 24.171-b02
18:47:59.722 INFO - OS: Linux 3.13.0-149-generic amd64
18:47:59.731 INFO - v2.13.0, with Core v2.13.0. Built from revision 14793
18:47:59.889 INFO - RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub
18:47:59.891 INFO - Version Jetty/5.1.x
18:47:59.892 INFO - Started HttpContext[/selenium-server/driver,/selenium-server/driver]
18:47:59.893 INFO - Started HttpContext[/selenium-server,/selenium-server]
18:47:59.893 INFO - Started HttpContext[/,/]
18:47:59.915 INFO - Started org.openqa.jetty.jetty.servlet.ServletHandler@6c15f918
18:47:59.915 INFO - Started HttpContext[/wd,/wd]
18:47:59.919 INFO - Started SocketListener on 0.0.0.0:4444
18:47:59.919 INFO - Started org.openqa.jetty.jetty.Server@606145c5

注意,特别是读取的行:

RemoteWebDriver instances should connect to: http://127.0.0.1:4444/wd/hub

command_executor中使用此网址:

from selenium import webdriver

driver = webdriver.Remote(
    desired_capabilities=webdriver.DesiredCapabilities.CHROME,
    command_executor='http://127.0.0.1:4444/wd/hub')
driver.get('http://www.google.com')

答案 1 :(得分:0)

根据 API文档Remote WebDriver实施如下:

class selenium.webdriver.remote.webdriver.WebDriver(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=None, browser_profile=None, proxy=None, keep_alive=False, file_detector=None, options=None)

example

# Instantiate an instance of Remote WebDriver with the desired capabilities.
driver = webdriver.Remote(desired_capabilities=capabilities, command_executor=selenium_grid_url)    

所以你需要改变代码行:

driver= webdriver.Remote(desired_capabilities=webdriver.DesiredCapabilities.CHROME,command_executor='http://127.0.0.1:4444')

要:

driver= webdriver.Remote(desired_capabilities=DesiredCapabilities().CHROME,command_executor='http://127.0.0.1:4444')