【问题标题】:How to change proxy settings using Python 2.7 in Mac OSX如何在 Mac OSX 中使用 Python 2.7 更改代理设置
【发布时间】:2016-07-03 02:36:35
【问题描述】:

我在 python 脚本中使用 selenium webdriver。我想在访问任何网站之前将代理设置从 python 更改为 vpn,以便在通过 webdriver 访问任何网站时,它们会根据 vpn ip 地址进行检测。

谁能帮我做这件事。提前感谢您的帮助。

【问题讨论】:

  • 我通过更改 Firefox 配置文件设置解决了这个问题。它没有做完全相同的事情。但足以满足我的要求。你也可以试试这个。 stackoverflow.com/a/38168865/5409601

标签: macos python-2.7 selenium proxy webdriver


【解决方案1】:

在使用python绑定时,需要使用远程对象来设置代理。

另一种方法是更改​​python绑定代码本身。

以下代码将更改代理

from selenium import webdriver

PROXY = "localhost:8080"

# Create a copy of desired capabilities object.
desired_capabilities = webdriver.DesiredCapabilities.INTERNETEXPLORER.copy()
# Change the proxy properties of that copy.
desired_capabilities['proxy'] = {
    "httpProxy":PROXY,
    "ftpProxy":PROXY,
    "sslProxy":PROXY,
    "noProxy":None,
    "proxyType":"MANUAL",
    "class":"org.openqa.selenium.Proxy",
    "autodetect":False
}

# you have to use remote, otherwise you'll have to code it yourself in python to 
# dynamically changing the system proxy preferences
driver = webdriver.Remote("http://localhost:4444/wd/hub", desired_capabilities)

【讨论】:

  • 我在 Windows 8.1 中收到此错误:URLError: <urlopen error [Errno 10061] No connection could be made because the target machine actively refused it>,在 Mac OSX 中收到此错误urllib2.URLError: <urlopen error [Errno 61] Connection refused>
  • 硒网格是否启动并运行?
  • 不用担心。我从其他地方得到了解决方案。硒网站上的文档可能不是最新的。我在这里写了解决方案。 stackoverflow.com/a/38168865/5409601
猜你喜欢
  • 2012-07-28
  • 2017-03-08
  • 2016-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-20
  • 2011-05-09
相关资源
最近更新 更多