简体   繁体   中英

Python Selenium Phantomjs, set proxy at runtime

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?

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 ;)

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