Proxy: Selenium + Python, Firefox

后端 未结 3 470
谎友^
谎友^ 2020-12-05 08:10

How can I redirect the traffic of Firefox launched by Selenium in Python to a proxy? I have used the solutions suggested on the web but they don\'t work!

I have tri

相关标签:
3条回答
  • 2020-12-05 08:49

    Try this for firefox/geckodriver:

    proxy = "212.66.117.168:41258"
    
    firefox_capabilities = webdriver.DesiredCapabilities.FIREFOX
    firefox_capabilities['marionette'] = True
    
    firefox_capabilities['proxy'] = {
        "proxyType": "MANUAL",
        "httpProxy": proxy,
        "ftpProxy": proxy,
        "sslProxy": proxy
    }
    
    driver = webdriver.Firefox(capabilities=firefox_capabilities)
    

    And for Chrome you can use:

    proxy = "212.66.117.168:41258"
    prox = Proxy()
    prox.proxy_type = ProxyType.MANUAL
    prox.http_proxy = proxy
    prox.socks_proxy = proxy
    prox.ssl_proxy = proxy
    
    capabilities = webdriver.DesiredCapabilities.CHROME
    prox.add_to_capabilities(capabilities)
    driver = webdriver.Chrome(desired_capabilities=capabilities)
    
    0 讨论(0)
  • 2020-12-05 09:01

    You need to import the following:

    from selenium.webdriver.common.proxy import Proxy, ProxyType
    

    Then setup the proxies:

    myProxy = "xx.xx.xx.xx:xxxx"
    
    proxy = Proxy({
        'proxyType': ProxyType.MANUAL,
        'httpProxy': myProxy,
        'ftpProxy': myProxy,
        'sslProxy': myProxy,
        'noProxy': '' # set this value as desired
        })
    

    Then call the webdriver.Firefox() function as follows:

    driver = webdriver.Firefox(proxy=proxy)
    driver.get("http://www.google.com")
    

    Don't remember where exactly I found this solution, but its out there somewhere. Will surely provide a link if I find it again. Just took this part out of my code.

    0 讨论(0)
  • 2020-12-05 09:07

    Your problem is on the driver init. Try webdriver = webdriver.Firefox(firefox_profile=profile) All the other code is OK and you can also remove profile.update_preferences() line.

    I found YOUR solution with a 2 minute google search. How much time did you spend waiting for? :D

    Your problem is that you maybe read code from other langs but Python. Replace this webdriver.Firefox(profile) with webdriver.Firefox(firefox_profile=profile).

    Your code should be:

    profile = webdriver.FirefoxProfile() 
    profile.set_preference("network.proxy.type", 1)
    profile.set_preference("network.proxy.http", "54.213.66.208")
    profile.set_preference("network.proxy.http_port", 80)
    profile.update_preferences() 
    driver = webdriver.Firefox(firefox_profile=profile)
    
    0 讨论(0)
提交回复
热议问题