如何在Selenium中通过授权设置socks5代理?

时间:2019-07-06 11:19:07

标签: python-3.x selenium selenium-webdriver

我这样配置代理:

proxy_opts = Proxy()
proxy_opts.proxy_type = ProxyType.MANUAL
proxy_opts.socks_proxy = 'socks5://localhost:9050'
proxy_opts.socks_username = 'foo'
proxy_opts.socks_password = 'bar'
capabilities = DesiredCapabilities.CHROME
proxy_opts.add_to_capabilities(capabilities)
options = Options()
options.add_experimental_option('excludeSwitches', ['enable-logging'])
driver = webdriver.Chrome(executable_path='libs/chromedriver.exe', options=options, desired_capabilities=capabilities)

回溯(最近通话最近一次):

File "registration.py", line 249, in 
registration()
File "registration.py", line 182, in registration
driver = webdriver.Chrome(executable_path='libs/chromedriver.exe', options=options, desired_capabilities=capabilities)
File "C:\Python37\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 81, in __init__
desired_capabilities=desired_capabilities)
File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
self.start_session(capabilities, browser_profile)
File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: cannot parse capability: proxy
from invalid argument: Specifying 'socksProxy' requires an integer for 'socksVersion'

另一个变体:

options.add_argument('--proxy-server=socks5://{}@localhost:9050'.format('foo:bar'))
driver = webdriver.Chrome(executable_path='libs/chromedriver.exe', options=options)

在这种情况下,没有错误,但是当您启动浏览器时,立即显示“无法加载页面。”

未经授权的选项有效(但不适合我)。

之后,我尝试在Firefox中使用

proxy = '127.0.0.1:9050'
capabilities = DesiredCapabilities.FIREFOX
capabilities['proxy'] = {'proxyType': 'MANUAL', 
                         'socksProxy': proxy, 
                         'socksVersion': 5}
# capabilities['proxy']['socksUsername'] = 'foo')
# capabilities['proxy']['socksPassword'] = 'bar')
driver  = webdriver.Firefox(desired_capabilities=capabilities, executable_path='libs/geckodriver.exe')

使用socksUsername和socksPassword我有错误: 追溯(最近一次通话):

  File "registration.py", line 275, in <module>
    registration('FIREFOX')
  File "registration.py", line 198, in registration
    driver  = webdriver.Firefox(desired_capabilities=capabilities, executable_path='libs/geckodriver.exe')
  File "C:\Python37\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 174, in __init__
    keep_alive=True)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.InvalidArgumentException: Message: Invalid proxy configuration entry: socksPassword

没有socksUsername和socksPassword,它与Chrome一样运行良好。 我不在乎在哪里解决这个问题:Chrome或Firefox,我只需要一个解决方案。

0 个答案:

没有答案