Python Selenium Phantomjs, set proxy at runtime
I am aware that I can set proxy parameters to initialize phantomjs using service_args, but restarting phantomjs every time changing proxy settings seems wasteful. In javascript changing the proxy at runtime, the setProxy function will be executed. How can I make this work in Python using selenium?
+3
source to share
1 answer
After trying different options and reading a little code, I realized that it is possible to dynamically change proxies in python + selenium + phantomjs. For posterity, here's a sample code:
from selenium import webdriver
driver = webdriver.PhantomJS()
driver.command_executor._commands['executePhantomScript'] = ('POST', '/session/$sessionId/phantom/execute')
driver.execute('executePhantomScript', {'script': '''phantom.setProxy("10.0.0.1", 80);''', 'args' : [] })
Happy halos;)
+3
source to share