【问题标题】:How to configure special proxy settings for a remote selenium webdrive with python?如何使用 python 为远程 selenium webdriver 配置特殊代理设置?
【发布时间】:2018-12-14 09:02:35
【问题描述】:

我将首先描述我正在使用的基础架构。它包含多个代理服务器,这些代理服务器使用负载平衡器将用户身份验证转发到直接绑定到活动目录的适当代理。身份验证使用用于登录到发出请求的计算机的凭据和源 IP。服务器将 IP 和凭据缓存 60 分钟。我正在使用专门用于此过程的测试帐户,并且仅在单元测试服务器上使用。

我正在使用 docker 容器在远程服务器上使用 selenium webdriver 进行一些自动化。我使用 python 作为脚本语言。我正在尝试在内部和外部网页/应用程序上运行测试。我能够使用以下脚本在内部网站上进行基本测试:

注意:10.1.54.118 是托管带有 selenium Web 驱动程序的 docker 容器的服务器

from selenium import webdriver

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

browser = webdriver.Remote(command_executor='http://10.1.54.118:4444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME)
browser.get("http://10.0.0.2")

print (browser.find_element_by_tag_name('body').text)

bodyText = browser.find_element_by_tag_name('body').text

print (bodyText)
    
if 'Hello' in bodyText:
    print ('Found hello in body')
else:
    print ('Hello not found in body')

browser.quit()

该脚本能够访问内部网页并打印其上的所有文本。

但是,我在尝试针对外部网站运行测试脚本时遇到了问题。

我尝试了以下文章和教程,但似乎对我不起作用。

我尝试过的文章和教程:

我尝试创建 4 个版本的脚本来访问外部站点,即 google.com,然后简单地打印出其中的文本。每个脚本都会返回一个超时错误。对于发布大量代码,我深表歉意,但也许社区能够看到我在编码方面出了什么问题。

代码 1:

from selenium import webdriver

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

PROXY = "10.32.51.169:3128" # IP:PORT or HOST:PORT
desired_capabilities = webdriver.DesiredCapabilities.CHROME.copy()

desired_capabilities['proxy'] = {
    "httpProxy":PROXY,
    "ftpProxy":PROXY,
    "sslProxy":PROXY,
    "socksUsername":"myusername",
    "socksPassword":"mypassword",
    "noProxy":None,
    "proxyType":"MANUAL",
    "class":"org.openqa.selenium.Proxy",
    "autodetect":False
    }
browser = webdriver.Remote('http://10.1.54.118:4444/wd/hub', desired_capabilities)
browser.get("https://www.google.com/")

print (browser.find_element_by_tag_name('body').text)

bodyText = browser.find_element_by_tag_name('body').text

print (bodyText)
   
if 'Hello' in bodyText:
    print ('Found hello in body')
else:
    print ('Hello not found in body')

browser.quit()

我的代码有什么不正确的吗?我是否能够将配置参数传递给 docker chrome selenium webdriver,或者我是否需要在构建之前使用预先配置的代理设置来构建 docker 容器?我期待您的回复和任何可以为我指明正确方向的帮助。

【问题讨论】:

  • 我无法包含我在一篇文章中创建的其他代码以及日志。我将在此处添加它们:

标签: python docker selenium-webdriver http-proxy remotewebdriver


【解决方案1】:

这个有点晚了,但有几个想法+改进:

  1. 从 socks 代理配置中删除用户/密码,并将它们添加到您的代理连接 uri。
  2. 使用 selenium Proxy 对象来帮助抽象代理功能的其他一些位。
  3. 将方案添加到代理连接字符串。
  4. 使用 try/finally 块来确保浏览器在任何失败的情况下都会退出

注意...我使用的是 Python3,selenium 版本 3.141.0,为了简洁/简单起见,我省略了 FTP 配置:

from selenium import webdriver

from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import Proxy

# Note the addition of the scheme (http) and the user/pass into the connection string.    
PROXY = 'http://myusername:mypassword@10.32.51.169:3128'

# Use the selenium Proxy object to add proxy capabilities
proxy_config = {'httpProxy': PROXY, 'sslProxy': PROXY}
proxy_object = Proxy(raw=proxy_config)
capabilities = DesiredCapabilities.CHROME.copy()
proxy_object.add_to_capabilities(capabilities)

browser = webdriver.Remote('http://10.1.54.118:4444/wd/hub', desired_capabilities=capabilities)

# Use try/finally so the browser quits even if there is an exception
try:
    browser.get("https://www.google.com/")

    print(browser.find_element_by_tag_name('body').text)

    bodyText = browser.find_element_by_tag_name('body').text

    print(bodyText)

    if 'Hello' in bodyText:
        print('Found hello in body')
    else:
        print('Hello not found in body')
finally:
    browser.quit()

【讨论】:

    猜你喜欢
    • 2015-07-29
    • 2019-04-04
    • 2012-10-01
    • 2016-11-08
    • 2015-03-19
    • 2019-08-07
    • 1970-01-01
    • 1970-01-01
    • 2019-01-19
    相关资源
    最近更新 更多