суббота, 27 августа 2016 г.

Phantomjs with proxy and user agent using java

        

How to implement phantomjs using java

Download library for windows from the official site http://phantomjs.org/download.html
Phantomjs maven repo https://mvnrepository.com/artifact/com.codeborne/phantomjsdriver
Extract phantomjs.exe to the current working directory

public static Capabilities getCapabilities() {
        DesiredCapabilities cap = new DesiredCapabilities();
        String proxy = "--proxy=ip:port;"; //if you need proxy
        String auth = "--proxy-auth=login:pass"; //if you need proxy
        String type = "--proxy-type=http"; //if you need proxy
        String[] phantomArgs = new String[]{
                "--webdriver-loglevel=NONE",
                proxy, auth, type

        };
        String fileName = "phantomjs.exe";
        cap.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, phantomArgs);
     cap.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, fileName);
cap.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "userAgent",  "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:39.0) Gecko/20100101 Firefox/39.0");
        return cap;
    }

PhantomJSDriver driver = new PhantomJSDriver(getCapabilities());

That's all

Комментариев нет:

Отправить комментарий