如何使用python配置远程Selenium WebDrive的特殊代理设置? [英] How to configure special proxy settings for a remote selenium webdrive with python?

查看:316
本文介绍了如何使用python配置远程Selenium WebDrive的特殊代理设置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I will start by describing the infrastructure I am working within. It contains multiple proxy servers that uses a load balancer to forward user authentications to the appropriate proxy that are directly tied to an active directory. The authentication uses the credentials and source IP that was used to log into the computer the request is coming from. The server caches the IP and credentials for 60 minutes. I am using a test account specifically for this process and is only used on the unit testing server.

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

I am working on some automation with selenium webdriver on a remote server using a docker container. I am using python as the scripting language. I am trying to run tests on both internal and external webpages/applications. I was able to get a basic test on an internal website with the following script:

注意: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()

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

The script is able to access the internal webpage and print all the text on it.

但是,尝试在外部网站上运行测试脚本时遇到问题.

However, I am experiencing problems trying to run test scripts against external websites.

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

I have tried the following articles and tutorials and it doesn't seem to work for me.

我尝试过的文章和教程:

The articles and tutorials I have tried:

  • https://www.seleniumhq.org/docs/04_webdriver_advanced.jsp
  • Pass driver ChromeOptions and DesiredCapabilities?
  • https://www.programcreek.com/python/example/100023/selenium.webdriver.Remote
  • https://github.com/webdriverio/webdriverio/issues/324
  • https://www.programcreek.com/python/example/96010/selenium.webdriver.common.desired_capabilities.DesiredCapabilities.CHROME
  • Running Selenium Webdriver with a proxy in Python
  • how do i set proxy for chrome in python webdriver
  • https://docs.proxymesh.com/article/4-python-proxy-configuration

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

I have tried creating 4 versions of a script to access an external site i.e. google.com and simply print the text off of it. Every script returns a time out error. I apologize for posting a lot of code but maybe the community is able to see where I am going wrong with the coding aspect.

代码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容器?我期待着您的答复以及能为我指明正确方向的任何帮助.

Is my code incorrect in any way? Am I able to pass configuration parameters to the docker chrome selenium webdriver or do I need to build the docker container with the proxy settings preconfigured before building it? I look forward to your replies and any help that can point me in the right direction.

推荐答案

对此稍晚一些,但有一些想法和改进之处:

A little late on this one, but a couple ideas + improvements:

  1. 从socks代理配置中删除用户/密码,并将其添加到代理连接uri.
  2. 使用硒代理对象来帮助抽象代理功能的其他一些位.
  3. 将该方案添加到代理连接字符串中.
  4. 使用try/finally块来确保浏览器即使出现任何故障也可以退出

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

Note... I'm using Python3, selenium version 3.141.0, and I'm leaving out the FTP config for brevity/simplicity:

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配置远程Selenium WebDrive的特殊代理设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆