Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set proxy authentication in PhantomJS using selenium?

I'm running this simple selenium test in java:

public static void main(String[] args){
    WebDriver driver = new PhantomJSDriver();
    driver.get("http://www.google.com");
    WebElement element = driver.findElement(By.id("gbqfif"));
    element.sendKeys("cheese");
    element.submit();
    System.out.println("Titulo:"+driver.getTitle());
    driver.quit();
}

but here at my office it requires proxy authentication and I have no idea how to set it.

I have to put my user and password somewhere.

Can you help me out?

like image 898
Bruno Sousa Avatar asked Nov 14 '14 13:11

Bruno Sousa


People also ask

Does Selenium support PhantomJS?

Just like any other browsers with GUI interface (Firefox, IE, Chrome, etc.), for PhantomJS also, Selenium has a standard API to support the automation. The above code snippet launches Selenium official website on the PhantomJS browser and performs click operation on the download tab.

Can we handle a proxy using Selenium in Java?

We can handle proxy in Selenium in Java with the help of PROXY class.

Which of the following code is used in Selenium to configure the use of proxy?

Following piece of code used to set proxy in Selenium. ChromeOptions option = new ChromeOptions(); Proxy proxy = new Proxy(); proxy. setHttpProxy("localhost:5555"); option. setCapability(CapabilityType.


1 Answers

PhantomJS uses the three proxy options that are set from the commandline (docs).

  • --proxy=address:port specifies the proxy server to use (e.g. --proxy=192.168.1.42:8080).
  • --proxy-type=[http|socks5|none] specifies the type of the proxy server (default is http).
  • --proxy-auth specifies the authentication information for the proxy, e.g. --proxy-auth=username:password).

To use these, you have to add them to the DesiredCapabilities map (as seen in this answer):

ArrayList<String> cliArgsCap = new ArrayList<String>();
cliArgsCap.add("--proxy=address:port");
cliArgsCap.add("--proxy-auth=username:password");
cliArgsCap.add("--proxy-type=http");
DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
capabilities.setCapability(
    PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);
WebDriver driver = new PhantomJSDriver(capabilities);
like image 141
Artjom B. Avatar answered Oct 10 '22 02:10

Artjom B.