V
V
vyr0d0k2020-10-30 16:37:08
Python
vyr0d0k, 2020-10-30 16:37:08

Selenium Python how to change proxy?

I implement the automation of the payment system through selenium. Since a proxy is attached to each wallet, then when changing the wallet, you need to change the proxy (if I understand correctly, this can only be done by restarting the browser). I just can't figure out how to implement it. driver.quit() closes the browser and then everything. I don't know how to start it back. And so that it works 24/7, without user intervention. Under certain conditions, the account changed, and with it the proxy.

In general, the essence of the question is how to implement a proxy change within one driver without using Proxyfier and the like, using only Python and Selenium tools.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vyr0d0k, 2020-10-30
@vyr0d0k

In general, I solved the problem, here is the code:

class Driver:

    options = webdriver.FirefoxOptions()

    options.set_preference('dom.webdriver.enabled', False)
    options.set_preference('media.volume_scale', '0.0')
    options.set_preference('general.useragent.override', user_agent)

    profile = webdriver.FirefoxProfile()

    profile.set_preference('network.proxy.type', 1)
    profile.set_preference('network.proxy.http', proxy["ip"])
    profile.set_preference('network.proxy.http_port', proxy["http_port"])
    profile.set_preference('network.proxy.https', proxy["ip"])
    profile.set_preference('network.proxy.https_port', proxy["http_port"])
    profile.set_preference('network.proxy.ssl', proxy["ip"])
    profile.set_preference('network.proxy.ssl_port', proxy["http_port"])
    profile.set_preference('network.proxy.ftp', proxy["ip"])
    profile.set_preference('network.proxy.ftp_port', proxy["http_port"])
    profile.set_preference('network.proxy.socks', proxy["ip"])
    profile.set_preference('network.proxy.socks_port', proxy["socks_port"])

    driver = webdriver.Firefox(firefox_profile=profile, options=options)

    @classmethod
    def change_proxy(cls):
        cls.profile.set_preference('network.proxy.http', ip)
        cls.profile.set_preference('network.proxy.http_port', port)
        cls.profile.set_preference('network.proxy.https', ip)
        cls.profile.set_preference('network.proxy.https_port', port)
        cls.profile.set_preference('network.proxy.ssl', ip)
        cls.profile.set_preference('network.proxy.ssl_port', port)
        cls.profile.set_preference('network.proxy.ftp', ip)
        cls.profile.set_preference('network.proxy.ftp_port', port)
        cls.profile.set_preference('network.proxy.socks', ip)
        cls.profile.set_preference('network.proxy.socks_port', socks_port)
        cls.profile.update_preferences()
        cls.driver.quit()
        cls.driver = webdriver.Firefox(firefox_profile=cls.profile, options=cls.options)
        cls.driver.get("https://2ip.ru/")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question