3

我的目标是在 Selenium webdriver 中应用代理网关(例如 geosurf.io)。

  1. 我需要通过使用DesiredCapabilities来做到这一点,因为似乎DesiredCapabilities是插入代理 [gateway] ( source ) 的唯一方法。
  2. DesiredCapabilities功能在 Selenium Grid 中工作(不仅仅是在普通的 Selenium 服务器中)。硒网格文档
  3. 我已经在本地 Windows 10 机器上成功运行了 Selenium Grid。 在此处输入图像描述

  4. 因此,我编写了以下代码来为 Selenium webdriver 中使用的功能应用DesiredCapabilities和代理网关:

    import requests
    from selenium import webdriver
    from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
    PROXY = "gw1.geosurf.io:8080" # my account at geosurf.io, port 8080 - Germany
    
    from selenium.webdriver.common.proxy import Proxy, ProxyType
    proxy_object = Proxy()
    proxy_object.proxy_type = ProxyType.MANUAL
    proxy_object.http_proxy = PROXY
    proxy_object.socks_proxy = PROXY
    proxy_object.ssl_proxy = PROXY
    
    keep_alive = True
    browser_profile=None
    
    webdriver.DesiredCapabilities.FIREFOX = {
        "class":"org.openqa.selenium.Proxy",
        "autodetect":False,
        "platform": "WIN10"
    }
    driver = webdriver.Remote("http://192.168.43.98:5566/grid/register", webdriver.DesiredCapabilities.FIREFOX, browser_profile, proxy_object, keep_alive)
    

在运行上述代码时,我已经强制输出 webdriver.py (C:\Python27\Lib\site-packages\selenium\webdriver\remote\webdriver.py__init__ )内部的一些内容,在:

command_executor:  http://192.168.43.98:5566/grid/register
capabilities: 
{'autodetect': False,
 'class': 'org.openqa.selenium.Proxy',
 'platform': 'WIN10',
 'proxy': {'httpProxy': 'gw1.geosurf.io:8080',
           'proxyType': 'MANUAL',
           'socksProxy': 'gw1.geosurf.io:8080',
           'sslProxy': 'gw1.geosurf.io:8080'}}

然而问题变成了webdriver.py

Traceback (most recent call last):
  File "C:\Users\User\Documents\RnD\captcha-test\test_geosurf_proxy_gateway.py", line 21, in <module>
    driver = webdriver.Remote("http://192.168.43.98:5566/grid/register", webdriver.DesiredCapabilities.FIREFOX, browser_profile, proxy_object, keep_alive)
  File "C:\Python27\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 99, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Python27\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 191, in start_session
    self.session_id = response['sessionId']
TypeError: string indices must be integers

错误TypeError: string indices must be integers似乎与proxy gateway类型无关,也DesiredCapabilities与设置无关。

在第 190 行输出时,response变量是一个字符串,包含 html 片段:

     <!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <link rel="stylesheet" type="text/css" href="/assets/displayhelpservlet.css" media="all"/>
  <link href="/assets/favicon.ico" rel="icon" type="image/x-icon" />
  <script src="/assets/jquery-3.1.1.min.js" type="text/javascript"></script>
  <script src="/assets/displayhelpservlet.js" type="text/javascript"></script>
  <script type="text/javascript">
    var json = Object.freeze('{"version":"3.4.0","type":"Grid Node","consoleLink":"/wd/hub"}');
  </script>
</head>
<body>

<div id="content">
  <div id="help-heading">
    <h1><span id="logo"></span></h1>
    <h2>Selenium <span class="se-type"></span>&nbsp;v.<span class="se-version"></span></h2>
  </div>

  <div id="content-body">
    <p>
      Whoops! The URL specified routes to this help page.
    </p>
    <p>
      For more information about Selenium <span class="se-type"></span> please see the
      <a class="se-docs">docs</a> and/or visit the <a class="se-wiki">wiki</a>.
      <span id="console-item">
        Or perhaps you are looking for the Selenium <span class="se-type"></span> <a class="se-console">console</a>.
      </span>
    </p>
    <p>
      Happy Testing!
    </p>
  </div>

  <div>
    <footer id="help-footer">
      Selenium is made possible through the efforts of our open source community, contributions from
      these <a href="https://github.com/SeleniumHQ/selenium/blob/master/AUTHORS">people</a>, and our
      <a href="http://www.seleniumhq.org/sponsors/">sponsors</a>.
   </footer>
  </div>
 </div>

</body>
</html>

如何解决这个webdriver.py问题?

更新

当进一步调试时webdriver.py,我在之后输出response变量response = self.execute(Command.NEW_SESSION, parameters)

{'status': 0,
 'value': u'<!DOCTYPE html>\n<html lang="en">\n<head>\n  <meta charset="UTF-8">\n  <link rel="stylesheet" type="text/css" href="/assets/displayhelpservlet.css" media="all"/>\n  <link href="/assets/favicon.ico" rel="icon" type="image/x-icon" />\n  <script src="/assets/jquery-3.1.1.min.js" type="text/javascript"></script>\n  <script src="/assets/displayhelpservlet.js" type="text/javascript"></script>\n  <script type="text/javascript">\n    var json = Object.freeze(\'{"version":"3.4.0","type":"Grid Node","consoleLink":"/wd/hub"}\');\n  </script>\n</head>\n<body>\n\n<div id="content">\n  <div id="help-heading">\n    <h1><span id="logo"></span></h1>\n    <h2>Selenium <span class="se-type"></span>&nbsp;v.<span class="se-version"></span></h2>\n  </div>\n\n  <div id="content-body">\n    <p>\n      Whoops! The URL specified routes to this help page.\n    </p>\n    <p>\n      For more information about Selenium <span class="se-type"></span> please see the\n      <a class="se-docs">docs</a> and/or visit the <a class="se-wiki">wiki</a>.\n      <span id="console-item">\n        Or perhaps you are looking for the Selenium <span class="se-type"></span> <a class="se-console">console</a>.\n      </span>\n    </p>\n    <p>\n      Happy Testing!\n    </p>\n  </div>\n\n  <div>\n    <footer id="help-footer">\n      Selenium is made possible through the efforts of our open source community, contributions from\n      these <a href="https://github.com/SeleniumHQ/selenium/blob/master/AUTHORS">people</a>, and our\n      <a href="http://www.seleniumhq.org/sponsors/">sponsors</a>.\n   </footer>\n  </div>\n </div>\n\n</body>\n</html>'}

为什么它不包含sessionIdkey-value ?

更新 2

我的部分成功是跑步

`driver = webdriver.Remote("http://192.168.43.98:5566/wd/hub", webdriver.DesiredCapabilities.FIREFOX, browser_profile, proxy_object, keep_alive)` 

作为脚本的最后一行。它产生了以下错误:

Traceback (most recent call last):
  File "C:\Users\User\Documents\RnD\captcha-test\test_geosurf_proxy_gateway.py", line 21, in <module>
    driver = webdriver.Remote("http://192.168.43.98:5566/wd/hub", webdriver.DesiredCapabilities.FIREFOX, browser_profile, proxy_object, keep_alive)
  File "C:\Python27\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 101, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Python27\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 193, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Python27\Lib\site-packages\selenium\webdriver\remote\webdriver.py", line 265, in execute
    self.error_handler.check_response(response)
  File "C:\Python27\Lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
WebDriverException: Message: The best matching driver provider org.openqa.selenium.ie.InternetExplorerDriver can't create a new driver instance for Capabilities [{proxy={httpProxy=gw1.geosurf.io:8080, proxyType=MANUAL, socksProxy=gw1.geosurf.io:8080, sslProxy=gw1.geosurf.io:8080}, autodetect=false, class=org.openqa.selenium.Proxy, platform=WIN10}]
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'DESKTOP-78JS3VQ', ip: '192.168.43.98', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_131'
Driver info: driver.version: unknown
Stacktrace:
    at org.openqa.selenium.remote.server.DefaultDriverFactory.newInstance (DefaultDriverFactory.java:62)
    at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call (DefaultSession.java:222)
    at org.openqa.selenium.remote.server.DefaultSession$BrowserCreator.call (DefaultSession.java:209)
    at java.util.concurrent.FutureTask.run (None:-1)
    at org.openqa.selenium.remote.server.DefaultSession$1.run (DefaultSession.java:176)
    at java.util.concurrent.ThreadPoolExecutor.runWorker (None:-1)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run (None:-1)
    at java.lang.Thread.run (None:-1)

将代理地址更改为localhost:8080带来了同样的错误......

更新 3

我已成功在浏览器http://192.168.43.98:5566/wd/hub/static/resource/hub.html上手动启动/打开节点控制台 在此处输入图像描述

然而,我唯一可以加载的会话是 Chrome 浏览器 在此处输入图像描述

无法成功为此网格加载 FireFox 或 IE 10 浏览器会话: 在此处输入图像描述

我不知道如何管理外部代理插入的网格节点是否有帮助。

4

1 回答 1

0

最终,我可以使用以下代码打开 Chrome 浏览器实例:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
PROXY = "gw1.geosurf.io:8080" # 8080 - Germany

from selenium.webdriver.common.proxy import Proxy, ProxyType
proxy_object = Proxy()
proxy_object.proxy_type = ProxyType.MANUAL
proxy_object.http_proxy = PROXY
proxy_object.socks_proxy = PROXY
proxy_object.ssl_proxy = PROXY

keep_alive = True
browser_profile=None

capabilities = webdriver.DesiredCapabilities.CHROME.copy()
capabilities['class'] = "org.openqa.selenium.Proxy"
capabilities['platform'] = "WINDOWS"
capabilities['version'] = "10"
capabilities["autodetect"]= False

driver = webdriver.Remote("http://192.168.43.98:5566/wd/hub", capabilities, browser_profile, proxy_object, keep_alive)
driver.get('http://testing-ground.scraping.pro/recaptcha')
raw_input('Press any key to quit Selenium driver: ')
driver.quit()

然而,打开的浏览器实例无法加载任何内容......

于 2017-06-28T12:58:34.850 回答