这是我用来通过Selenium Webdriver(Java)启动Firefox的代码:
private FirefoxDriver getfox(String pr) {
String geckoPath = "/opt/driver";
String browserPath = "/opt/browser";
String h = pr.split(":")[0];
String p = pr.split(":")[1];
System.setProperty("webdriver.firefox.marionette", geckoPath);
FirefoxProfile fp;
fp = new FirefoxProfile();
System.out.println("setting proxy " + h + ", port " + p);
fp.setPreference("network.proxy.http", h);
fp.setPreference("network.proxy.http_port", p);
FirefoxOptions fo = new FirefoxOptions();
fo.setProfile(fp);
fo.setBinary(browserPath);
FirefoxDriver driver = new FirefoxDriver(fo);
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30,TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(30,TimeUnit.SECONDS);
return driver;
}
但是,当我在启动的浏览器中输入about:config
并检查设置时,只更改了代理主机。 http_port
设置为0
。当我通过导航检查我的IP时,例如,
Webdriver driver = getfox("host.proxy.server:port");
String address = "https://www.find-ip.net";
//String address = "http://www.find-ip.net";
System.out.println("navigating to " + address);
driver.get(address);
System.out.println("searching elements");
String selector = ("div.ipcontent.pure-u-13-24");
List<WebElement> elems = driver.findElements(By.cssSelector(selector));
for (WebElement w : elems) {
System.out.println(w.getText());
}
到http(s)://www.find-ip.net
然后我看到了我的真实IP地址。我传递的价值是正确的。我在HtmlUnit驱动程序中设置该代理没有问题。
涉及的版本是:
我需要改变什么吗?