在Python中使用代理运行Selenium Webdriver

我想在Python中运行一个Selenium Webdriver脚本来完成一些基本的任务。 通过Selenium IDE界面(也就是说,当简单地获取GUI重复我的操作时),我可以让机器人完美地运行。 但是,当我将代码作为Python脚本导出并尝试从命令行执行时,Firefox浏览器将打开,但不能访问起始URL(错误返回到命令行,程序停止)。 这正在发生我无论什么网站等我试图访问。

为了演示目的,我在这里包含了一个非常基本的代码。 我不认为我正确地包含了代码的代码段,因为返回的错误似乎是由代理生成的。

任何帮助将非常感激。

下面的代码只是打开www.google.ie并search单词“selenium”。 对我来说,它打开一个空白的Firefox浏览器,并停止。

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.support.ui import Select from selenium.common.exceptions import NoSuchElementException import unittest, time, re from selenium.webdriver.common.proxy import * class Testrobot2(unittest.TestCase): def setUp(self): myProxy = "http://149.215.113.110:70" proxy = Proxy({ 'proxyType': ProxyType.MANUAL, 'httpProxy': myProxy, 'ftpProxy': myProxy, 'sslProxy': myProxy, 'noProxy':''}) self.driver = webdriver.Firefox(proxy=proxy) self.driver.implicitly_wait(30) self.base_url = "https://www.google.ie/" self.verificationErrors = [] self.accept_next_alert = True def test_robot2(self): driver = self.driver driver.get(self.base_url + "/#gs_rn=17&gs_ri=psy-ab&suggest=p&cp=6&gs_id=ix&xhr=t&q=selenium&es_nrs=true&pf=p&output=search&sclient=psy-ab&oq=seleni&gs_l=&pbx=1&bav=on.2,or.r_qf.&bvm=bv.47883778,d.ZGU&fp=7c0d9024de9ac6ab&biw=592&bih=665") driver.find_element_by_id("gbqfq").clear() driver.find_element_by_id("gbqfq").send_keys("selenium") def is_element_present(self, how, what): try: self.driver.find_element(by=how, value=what) except NoSuchElementException, e: return False return True def is_alert_present(self): try: self.driver.switch_to_alert() except NoAlertPresentException, e: return False return True def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to_alert() alert_text = alert.text if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert_text finally: self.accept_next_alert = True def tearDown(self): self.driver.quit() self.assertEqual([], self.verificationErrors) if __name__ == "__main__": unittest.main() 

这样的事情呢?

 PROXY = "149.215.113.110:70" webdriver.DesiredCapabilities.FIREFOX['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 driver = webdriver.Remote("http://localhost:4444/wd/hub", webdriver.DesiredCapabilities.FIREFOX) 

你可以在这里阅读更多。

我的解决scheme

 def my_proxy(PROXY_HOST,PROXY_PORT): fp = webdriver.FirefoxProfile() # Direct = 0, Manual = 1, PAC = 2, AUTODETECT = 4, SYSTEM = 5 print PROXY_PORT print PROXY_HOST fp.set_preference("network.proxy.type", 1) fp.set_preference("network.proxy.http",PROXY_HOST) fp.set_preference("network.proxy.http_port",int(PROXY_PORT)) fp.set_preference("general.useragent.override","whater_useragent") fp.update_preferences() return webdriver.Firefox(firefox_profile=fp) 

然后调用你的代码:

 my_proxy(PROXY_HOST,PROXY_PORT) 

我有这个代码的问题,因为我传递一个string作为端口#:

  PROXY_PORT="31280" 

这个很重要:

 int("31280") 

您必须传递一个整数而不是一个string,否则您的firefoxconfiguration文件将不会被设置为正确的端口,并且通过代理连接将不起作用。

尝试设置sock5代理。 我正面临同样的问题,它是通过使用袜子代理解决

 def install_proxy(PROXY_HOST,PROXY_PORT): fp = webdriver.FirefoxProfile() print PROXY_PORT print PROXY_HOST fp.set_preference("network.proxy.type", 1) fp.set_preference("network.proxy.http",PROXY_HOST) fp.set_preference("network.proxy.http_port",int(PROXY_PORT)) fp.set_preference("network.proxy.https",PROXY_HOST) fp.set_preference("network.proxy.https_port",int(PROXY_PORT)) fp.set_preference("network.proxy.ssl",PROXY_HOST) fp.set_preference("network.proxy.ssl_port",int(PROXY_PORT)) fp.set_preference("network.proxy.ftp",PROXY_HOST) fp.set_preference("network.proxy.ftp_port",int(PROXY_PORT)) fp.set_preference("network.proxy.socks",PROXY_HOST) fp.set_preference("network.proxy.socks_port",int(PROXY_PORT)) fp.set_preference("general.useragent.override","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A") fp.update_preferences() return webdriver.Firefox(firefox_profile=fp) 

然后从程序中调用install_proxy ( ip , port )

适用于我这种方式(类似于@Amey和@ user4642224代码,但稍短一点):

 from selenium import webdriver from selenium.webdriver.common.proxy import Proxy, ProxyType prox = Proxy() prox.proxy_type = ProxyType.MANUAL prox.http_proxy = "ip_addr:port" prox.socks_proxy = "ip_addr:port" prox.ssl_proxy = "ip_addr:port" capabilities = webdriver.DesiredCapabilities.CHROME prox.add_to_capabilities(capabilities) driver = webdriver.Chrome(desired_capabilities=capabilities) 

如果有人正在寻找一个解决scheme,如何:

 from selenium import webdriver PROXY = "YOUR_PROXY_ADDRESS_HERE" webdriver.DesiredCapabilities.FIREFOX['proxy']={ "httpProxy":PROXY, "ftpProxy":PROXY, "sslProxy":PROXY, "noProxy":None, "proxyType":"MANUAL", "autodetect":False } driver = webdriver.Firefox() driver.get('http://www.whatsmyip.org/') 

尝试通过设置FirefoxProfile

 from selenium import webdriver import time "Define Both ProxyHost and ProxyPort as String" ProxyHost = "54.84.95.51" ProxyPort = "8083" def ChangeProxy(ProxyHost ,ProxyPort): "Define Firefox Profile with you ProxyHost and ProxyPort" profile = webdriver.FirefoxProfile() profile.set_preference("network.proxy.type", 1) profile.set_preference("network.proxy.http", ProxyHost ) profile.set_preference("network.proxy.http_port", int(ProxyPort)) profile.update_preferences() return webdriver.Firefox(firefox_profile=profile) def FixProxy(): ""Reset Firefox Profile"" profile = webdriver.FirefoxProfile() profile.set_preference("network.proxy.type", 0) return webdriver.Firefox(firefox_profile=profile) driver = ChangeProxy(ProxyHost ,ProxyPort) driver.get("http://whatismyipaddress.com") time.sleep(5) driver = FixProxy() driver.get("http://whatismyipaddress.com") 

该程序在Windows 8和Mac OSX上均经过testing 。 如果你使用Mac OSX,如果你没有更新selenium,那么你可能会面临selenium.common.exceptions.WebDriverException 。 如果是这样,那么在升级selenium后再试一次

 pip install -U selenium