简体   繁体   中英

How can I change geckodriver's proxy?

I need change Firefox driver's proxy when I start code because my project creates a lot of accounts in any sites and some sites blocks it for example Instagram and I need change proxy for this issue. How I can do it?

Changing the settings from FireFox Profile is probably what you are looking for webdriver.FirefoxProfile()

        from selenium import webdriver

        # myProxy = "127.0.0.1:9150"
        myProxy = "192.168.103.1:1081"
        ip, port = myProxy.split(":")
        fp = webdriver.FirefoxProfile()
        fp.set_preference('network.proxy.type', 1)
        fp.set_preference('network.proxy.socks', ip)
        fp.set_preference('network.proxy.socks_port', int(port))
        driver = webdriver.Firefox(fp)

From there you can change settings easily by calling .set_preference()

Another way for proxies from the official doc

from selenium import webdriver

PROXY = "<HOST:PORT>"
webdriver.DesiredCapabilities.FIREFOX['proxy'] = {
    "httpProxy": PROXY,
    "ftpProxy": PROXY,
    "sslProxy": PROXY,
    "proxyType": "MANUAL",

}

with webdriver.Firefox() as driver:
    # Open URL
    driver.get("https://selenium.dev")

Selenium faq doc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM