BrowserStack 脚本失败,并显示 MaxRetryError



来自 https://www.browserstack.com/automate/python 的脚本

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
desired_cap = {
    'browser': 'Chrome',
    'browser_version': '62.0',
    'os': 'Windows',
    'os_version': '10',
    'resolution': '1024x768',
    'name': 'Bstack-[Python] Sample Test'
}
driver = webdriver.Remote(
    command_executor='http://servinc1:key@hub.browserstack.com:80/wd/hub',
    desired_capabilities=desired_cap)
driver.get("http://www.google.com")
if not "Google" in driver.title:
    raise Exception("Unable to load google page!")
elem = driver.find_element_by_name("q")
elem.send_keys("BrowserStack")
elem.submit()
print driver.title
driver.quit()

失败与

urllib3.exceptions.MaxRetryError: HTTPConnectionPool(host='hub.browserstack.com', port=80(:url 超出最大重试次数:/wd/hub/session(由 NewConnectionError(': 无法建立新连接: [Errno 111] 连接被拒绝',((

在具有本地主机 HTTP 代理的系统上。代理配置了{http,https}_proxy环境变量:使用请求有效:

import requests
r = requests.get('https://api.github.com/events')

并且允许连接到hub.browserstack.com也可以。

目的是将BrowserStack与本地代理一起使用。这是如何解决的?

因此,目前解决方法似乎是答案:允许所有连接到 hub.browserstack.com 的连接通过防火墙。 例如

iptables -I OUTPUT 1 -p tcp --dport 443 -d hub.browserstack.com  -j ACCEPT

由于您的用例涉及使用代理将流量发送到 BrowserStack Hub,因此您需要在代码片段中指定代理详细信息,如下所示-

//Set the appropriate proxy environment variable (HTTP_PROXY if it is a HTTP proxy, HTTPS_PROXY if it is a HTTPS proxy, etc.) before running the tests.
//You can set this as follows:
export HTTP_PROXY='http://<proxyhost>:<proxyport>'

您可以在此处阅读更多内容- https://www.browserstack.com/automate/python#proxy

在为python Selenium安装程序安装依赖库时,您可以使用urllib3==1.24.3,它应该可以工作。

pip install urllib3==1.24.3

注意:此版本的urllib3与Selenium 4不兼容

最新更新