Macos 如何在Mac OSX中使用Python 2.7更改代理设置

Macos 如何在Mac OSX中使用Python 2.7更改代理设置,macos,python-2.7,selenium,proxy,webdriver,Macos,Python 2.7,Selenium,Proxy,Webdriver,我正在python脚本中使用SeleniumWebDriver。我想在访问任何网站之前将代理设置从python更改为vpn,这样,当通过webdriver访问任何网站时,他们都会根据vpn ip地址进行检测 谁能帮我做这件事。提前感谢你的帮助 在使用python绑定时,需要使用远程对象来设置代理 另一种方法是更改python绑定代码本身 下面的代码将更改代理 from selenium import webdriver PROXY = "localhost:8080" # Create a

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

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


在使用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)

在使用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:
而在Mac OSX中出现了这个错误
urlib2.URLError:
selenium grid是否已启动并运行?不用担心。我从别的地方得到了答案。selenium站点上的文档可能不是最新的。我已经在这里写出了解决方案。我在Windows 8.1中遇到了这个错误:
URLError:
而在Mac OSX中出现了这个错误
urlib2.URLError:
selenium grid是否已启动并运行?不用担心。我从别的地方得到了答案。selenium站点上的文档可能不是最新的。我已经在这里写出了解决方案。我通过更改Firefox配置文件设置解决了这个问题。它并不是在做完全相同的事情。但足够满足我的要求。你也可以试试这个。我通过更改Firefox配置文件设置解决了这个问题。它并不是在做完全相同的事情。但足够满足我的要求。你也可以试试这个。