python - 如何使用 python 为远程 selenium webdriver 配置特殊代理设置?

标签 python docker selenium-webdriver http-proxy remotewebdriver

我将首先描述我正在工作的基础设施。它包含多个代理服务器,这些代理服务器使用负载平衡器将用户身份验证转发到直接绑定(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()
该脚本能够访问内部网页并打印其上的所有文本。
但是,我在尝试针对外部网站运行测试脚本时遇到了问题。
我已经尝试了以下文章和教程,但它似乎对我不起作用。
我尝试过的文章和教程:
  • 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,然后简单地打印出其中的文本。每个脚本都会返回一个超时错误。我为发布大量代码而道歉,但也许社区能够看到我在编码方面出了什么问题。
    代码 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 容器?我期待您的回复和任何可以为我指明正确方向的帮助。

    最佳答案

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

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

  • 注意...我使用的是 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/

    相关文章:

    python - 无法在 MySQL 中存储压缩文本

    python - 如何在 django View 中使用参数运行 scrapy 蜘蛛

    filesystems - docker,docker 容器的 aufs 根目录在哪里

    docker - 在 dev、qa、uat 和生产中推广 docker 镜像的最佳实践

    java - 无法通过 selenium WebDriver 上的 "isDisplyed"检查 WebElement 是否存在

    python - 查找元素的上一次出现

    python - 我如何循环代码,直到用户输入更改 python 中的变量

    .net - Asp.Net Core 2.2 Docker HTTPS问题

    java - 在公司代理后面使用 Selenium RemoteWebDriver

    java - 如何在 chrome 中使用 selenium 拖放 canvas web 元素