简体   繁体   中英

How can i set proxy using selenium webdriver with 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 says: I know how to set proxy using FirefoxDriver but I don't wanna use UI browser because it's too slow to execute, so, searching in google and another page I don't found anything similar, if anyone know how open an Url with a proxy using HtmlUnitDriver, please help your answer will be useful for me, thanks.

I am using this for FirefoxDriver, i wanna 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);

This is the answer for my own question, i do the method that "@Raghav N" told me, Thanks a lot! :D (y)

I do this and work perfectly, and it can be tested because open www.find-ip.net and scrapped the actually proxy active, and its the same what i put.

Here the code Working, if you wanna test it, copy and paste in your proyect. Note: with class proxy 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 dont load maybe the proxy is down try with another.

I hope this will help you, bye! :)

Can you try with following code

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

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