Selenium Wire远程模式下如何获取正确的主机名或IP?
远程运行Selenium Wire时
addr配置疑问 问题场景
尝试以远程模式运行Selenium Wire驱动,对seleniumwire_options里的addr配置存在误解:
- 误将
addr设为Selenium Grid的完整连接地址(如http://hub.selenium-grid:4444/wd/hub) - 触发报错:
E seleniumwire.thirdparty.mitmproxy.exceptions.ServerException: Error starting proxy server: gaierror(-2, 'Name or service not known')
- 原错误配置代码:
options = { 'addr': 'hostname_or_ip' # 运行Selenium Wire的机器地址。如果远程会话在本地运行,请明确使用127.0.0.1而非localhost。 } driver = webdriver.Remote( command_executor='http://www.example.com', seleniumwire_options=options )
正确的addr配置说明
addr并非Selenium Grid的地址,它是运行当前Python代码(即Selenium Wire代理服务所在)的机器的IP或主机名,取值规则:
- 代码与Selenium Grid会话同机运行:填
127.0.0.1(优先用IP而非localhost,规避DNS解析异常) - 代码在本地、Grid在远程服务器:填本地机器的公网IP,或局域网内Grid可访问到的内网IP(如
192.168.x.x) - 代码与Grid同部署在服务器/容器:填服务器内网IP或容器自身IP
错误原因
你传入了带协议、端口和路径的完整Grid URL,但addr仅接受纯IP或主机名字符串,导致DNS无法解析无效地址,触发gaierror。
获取对应主机名/IP的方法
- 本地机器:Windows执行
ipconfig,Linux/macOS执行ip addr或ifconfig,提取对应网络接口的IP - 服务器/容器:执行
hostname -I查看内网IP,或通过云服务商控制台获取公网IP
内容的提问来源于stack exchange,提问作者user11419808
相关产品推荐
相关产品推荐

