我将首先描述我正在工作的基础设施。它包含多个代理服务器,这些代理服务器使用负载平衡器将用户身份验证转发到直接绑定(bind)到事件目录的适当代理。身份验证使用用于登录到发出请求的计算机的凭据和源 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 容器?我期待您的回复和任何可以为我指明正确方向的帮助。
最佳答案
这个有点晚了,但有几个想法+改进:
注意...我使用的是 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()
关于python - 如何使用 python 为远程 selenium webdriver 配置特殊代理设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51198774/