Phantomjs: Dynamically change the proxy settings

Created on 1 Apr 2016  路  6Comments  路  Source: ariya/phantomjs

Is there any way to bind a new proxy server to already running PhantomJS process?
I do websites testing from different locations and "restart" procedure is very expensive.

Most helpful comment

I found this issue when googling for "phantom set proxy". What I need is to set the proxy for each page, so this is how I'm doing it (in case anyone else has the same need):

page.setProxy("http://user:pass@proxy_ip_or_host:port/"); // with authentication;
page.setProxy("http://proxy_ip_or_host:port/"); // or with no authentication;

All 6 comments

Yes there is, you can use:
phantom.setProxy(host_or_IP, port, proxy_type, user_name, password);
host_or_IP only this argument is required others are optional
port is numeric value by default 80
proxy_type can be http (default) or socks5

Also this kind of question should be asked on phantom's board but also this functionality should be documented on the website though.

Can be the proxy set via page? What is the difference?

https://github.com/ariya/phantomjs/blob/db0758195614ceb8a000b88c4e24face8fd35d7c/src/webpage.cpp
In this case it accepts the proxy URL, doesn't it?

Yeah, it would be great to have such info documented for such a big project with so many users

Yes it can but it would set proxy only on that particular page instance on the other hand setting up proxy on phantom global object will set proxy globally on all present and future page instances except those that have their own proxy setting. I'm not sure if you can later revert page instance proxy back to global setting once you change it.
Also default port is 80 not 8080 (I fixed that in my comment above)

Ok, got it, thanks!
Btw, is it possible to unset the proxy?

phantom.setProxy("")

I found this issue when googling for "phantom set proxy". What I need is to set the proxy for each page, so this is how I'm doing it (in case anyone else has the same need):

page.setProxy("http://user:pass@proxy_ip_or_host:port/"); // with authentication;
page.setProxy("http://proxy_ip_or_host:port/"); // or with no authentication;
Was this page helpful?
0 / 5 - 0 ratings