setting proxy in selenium in python for Firefox Geckodriver

邮差的信 提交于 2019-12-04 14:52:14

Yaso's answer didn't work for me, instead i used this

proxyString = "Ip:port"

desired_capability = webdriver.DesiredCapabilities.FIREFOX
        desired_capability['proxy'] = {
            "proxyType": "manual",
            "httpProxy": proxyString,
            "ftpProxy": proxyString,
            "sslProxy": proxyString
        }

I spent hours finding an answer and I want to share it. The simple problem was in the proxy specification. Initially the proxy and port were one string

PROXY = "94.56.171.137:8080"

the answer should make the port as a number

PROXY = "94.56.171.137"
PORT = 8080

Here is the rest of the code

from selenium import webdriver

PROXY = "94.56.171.137"
PORT = 8080

class Proxy(object):        
    def __call__(self):    
        self.base_url = "https://whatismyip.com"
        print self.base_url        
        # https://github.com/mozilla/geckodriver
        # proxy json object
        desired_capability = webdriver.DesiredCapabilities.FIREFOX
        desired_capability['proxy']={
            "proxyType":"manual",
            "httpProxy":PROXY,
            "httpProxyPort": PORT,
            "ftpProxy":PROXY,
            "ftpProxyPort": PORT,
            "sslProxy":PROXY,
            "sslProxyPort" : PORT
        }        

        firefox_profile = webdriver.FirefoxProfile()
        firefox_profile.set_preference("browser.privatebrowsing.autostart", True)
        self.driver = webdriver.Firefox(executable_path='D:\Drivers\geckodriver',firefox_profile=firefox_profile,  capabilities=desired_capability) 

        self.driver.get(self.base_url)    

if __name__ == "__main__":    

    proxy_test = Proxy()
    proxy_test()    code here
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!