首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >https上的远程selenium网格不工作

https上的远程selenium网格不工作
EN

Stack Overflow用户
提问于 2018-08-24 07:25:28
回答 1查看 1.1K关注 0票数 0

我工作的公司要求/印象中,当使用我们在docker实例中托管在AWS中的远程selenium网格服务器时,我们所有的selenium测试流量都要通过https进行。

到目前为止,这似乎一直在工作,但使用的是selenium 3.14的最新版本。即使是最简单的测试,我现在也能收到100个"InsecureRequestWarnings“。我已经诊断出这些问题是由到selenium网格的https连接引起的,而不是测试目标url本身,因为如果我从本地selenium服务器和webdriver运行相同的测试,我不会得到相同的问题。

我使用的是:Python3.6.4、pytest 3.7.2、pytest-selenium 1.13、selenium 3.14 (本地和远程selenium网格)、chromedriver 2.41 (本地和远程)、certifi 2018.8.13、urllib3 1.23

从不同的窗口运行(服务器2008、windows 10等)

在运行下面的代码(基本上是我的conftest.py和一个简单的登录测试脚本的组合)时,我收到以下警告(其中62个是重复的)。

代码语言:javascript
复制
D:\Work\PyTestFramework\VirtEnv3_6\lib\site-packages\urllib3\connectionpool.py:857: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
    InsecureRequestWarning)

我的示例代码:

代码语言:javascript
复制
import pytest
import webdriverwrapper
import webdriverwrapper.wrapper
from webdriverwrapper import DesiredCapabilities

# testtools are just my reusable helper libraries
from testtools import credentials_helper, login_helper, appointment_helper



def pytest_addoption(parser):
    parser.addoption('--url', action='store', default='https://bookingportal.mycompany.com.au/OBP',
                     help='target machine url')


@pytest.fixture(scope='session')
def url(request):
    return request.config.getoption('url')


@pytest.fixture(scope='function')
def browser(request):
    desired_cap = DesiredCapabilities.CHROME
    desired_cap['chromeOptions'] = {}
    desired_cap['chromeOptions']['args'] = ['--disable-plugins', '--disable-extensions']
    desired_cap['browserName'] = 'chrome'
    desired_cap['javascriptEnabled'] = True
    hostname = "https://selenium.mygriddocker.com.au/wd/hub"
    executor = webdriverwrapper.wrapper.remote.remote_connection.RemoteConnection(hostname, resolve_ip=False)
    b = webdriverwrapper.Remote(executor, desired_cap)
    request.addfinalizer(lambda *args: b.quit())
    return b


@pytest.fixture(scope='function')
def driver(browser, url):
    driver = browser
    driver.set_window_size(1260, 1080)
    driver.get(url)
    return driver


# ------------ put test script here



@pytest.mark.usefixtures("driver")
def test_OBP_Login(driver):
    testId = 'LogIn01'
    credentials_list = credentials_helper.get_csv_data('OBP_LoginDetails.csv', testId)

    assert driver.title == 'Booking Portal'
    rslt1 = login_helper.login_user(driver, credentials_list)
    assert rslt1

    rslt2 = appointment_helper.logout_OBP(driver)
    assert rslt2

正如您所看到的,我调用的selenium网格服务器的地址是https://selenium.mygriddocker.com.au/wd/hub (而不是实际的地址)

并且我已经验证了它确实有一个由公共机构(Comodo)颁发的有效证书。注意:如果我回滚到urllib3和证书,则不会出现此问题,但我不确定它在什么时候停止工作

很多人一直在压制警告,但我真的希望证书能正常工作。

显而易见的解决方案是按照链接的建议添加证书验证,但问题是如何通过selenium RemoteConnection类发送连接池管理器参数?

来自https://urllib3.readthedocs.io/en/latest/user-guide.html#ssl的建议修复:

代码语言:javascript
复制
 import certifi
 import urllib3
 http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=certifi.where())

当我深入研究selenium库时,我发现了urllib3.PoolManager的用法。PoolManager位于urllib3.PoolManager模块中,但是在调用它时,它没有任何参数可以发送额外的urllib3.PoolManager信息。您可以在我的示例代码中看到executor对象是我调用remote_connection的地方(通过webdriverwrapper,在常规selenium中,路径是selenium/webdriver/remote/remote_connection )

RemoteConnection类没有设置我所能看到的PoolManager。

我只是尝试编辑remote_connection,希望将认证信息硬编码到文件中,希望以后能弄清楚如何包装它。因此,我在看到urllib3.PoolManager()的地方添加了cert_reqs='CERT_REQUIRED', ca_certs=certifi.where()行(我还将证书导入到该模块中)。幸运的是,它真的起作用了。现在我想不出怎么包装它了。

关于如何让远程selenium在https上工作,有没有其他的想法?

EN

回答 1

Stack Overflow用户

发布于 2018-08-24 07:25:28

所以我发现在selenium 3.13和3.14之间,他们将http请求库从httplib更改为urllib3,并且似乎没有适当地处理证书。

所以我发现我的选择是: 1)使用selenium 3.13客户端2)使用selenium 3.14客户端并禁用InsecureRequestWarnings导入urllib3 urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

3)使用selenium 3.14客户端并破解用urllib3.PoolManager(cert_reqs='CERT_REQUIRED',ca_certs=certifi.where()替换所有urllib3.PoolManager()实例的remote_connections.py文件)

另一件需要注意的事情是,我认为Pycharm缓存库,所以如果我执行pip更新,而不清除cache,我会得到混合的结果

4)以某种方式将remote_connection.py包装到我自己的库中,并导入添加了PoolManager参数的类-这超出了我的能力范围。

这不再是一个问题,但我的信息可以帮助其他一些在远程selenium调用上使用https时收到urllib3不安全警告的人。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51995537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档