I am aware that I can set proxy settings for phantomjs on initialization using service_args but restarting phantomjs every time just to change proxy setting seems wasteful. In javascript changing proxy at runtime would be done with setProxy function. How can I make this work in Python using selenium?
1 Answer
Trying various options and reading a bit of code, I realized that it is possible to dynamically change proxies in python + selenium + phantomjs. For posterity here is 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 ghosting ;)
execute_script()does not what you want, because it executes the given script on the page. You cannot set the proxy with it.