How can I set up a proxy using selenium webdriver with the browser HtmlunitDriver for Java?

My name is Leo and I am a java bot developer, I am using Selenium webdriver and Browser HtmlUnitDriver headless, my question is like my title: I know how to set a proxy using FirefoxDriver, but I don't want to use the UI browser because it too slow to execute, so when searching on google and other page I didn't find anything similar, if anyone knows how to open url with a proxy using HtmlUnitDriver please help, your answer will be helpful to me, thanks.

I am using this for FirefoxDriver, I want to do the same with HtmlUnitDriver.

org.openqa.selenium.Proxy proxy = new org.openqa.selenium.Proxy();
    proxy.setHttpProxy("198.2.202.49:80")
         .setFtpProxy("198.2.202.49:80")
         .setSslProxy("198.2.202.49:80");
    DesiredCapabilities cap = new DesiredCapabilities();
    cap.setCapability(CapabilityType.PROXY, proxy);
    WebDriver driver = new FirefoxDriver(cap);

      

+3


source to share


2 answers


This is the answer to my question, I am doing the method that "@Raghav N" told me, thanks a lot!: D (y)

I do this and it works great and it can be tested because we open www.find-ip.net and give up the active proxy and its the same as I put.

Here's the code working, if you want to test it copy and paste into your project. Note: with the proxy class you need to "import org.openqa.selenium.Proxy;"

HtmlUnitDriver driver = new HtmlUnitDriver(); 
Proxy proxy = new Proxy();
proxy.setHttpProxy("42.117.1.78:3128"); 
driver.setProxySettings(proxy);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.get("http://www.find-ip.net");
String ip = driver.findElement(By.xpath("//*[@id='ipbox']/div[1]/div[2]")).getText(); // Copia el texto del actual ip
String pais = driver.findElement(By.xpath("//*[@id='ipbox']/div[2]/div[2]")).getText(); //Copia el texto del actual Pais del proxy
System.out.println("ยป Ip Ficticio: " + ip +" - Country: " + pais);

      



Output:

Ip: 42.117.1.78       -        Country: Viet Nam

      

If the page doesn't load, the proxy might be trying with another one.

Hope this helps you, bye! :)

+2


source


Can you try the following code



HtmlUnitDriver driver = new HtmlUnitDriver(capabilities);
ArrayList<String> noProxyHosts = null;
driver.setHTTPProxy("198.2.202.49", 80, noProxyHosts);

      

0


source







All Articles