动态设置seleium WebDriver代理。
通过FirefoxProfile, 我们可以在生成FirefoxDriver的时候设置firefox将要使用的代理。
void visit(String proxyStr) { FirefoxProfile profile = new FirefoxProfile(); Proxy proxy = new Proxy(); proxy.setHttpProxy(proxyStr); profile.setProxyPreferences(proxy); WebDriver driver = new FirefoxDriver(profile); try { driver.get("http://www.baidu.com"); WebElement element = driver.findElement(By.id("kw")); element.sendKeys("广州办公家具 礼邦"); element.submit(); System.out.println("Page title is: " + driver.getTitle()); (new WebDriverWait(driver, 30)) .until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle() != null; } }); element = driver.findElement(By.linkText("广州礼邦办公家具有限公司")); element.click(); driver.switchTo(); (new WebDriverWait(driver, 30)) .until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { System.out .println("Page title is: " + d.getTitle()); return d.getTitle() != null; } }); } catch (Exception e) { System.out.println("proxy is not accessable"); } finally { } try { Thread.currentThread().sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out .println("Page title is: " + driver.getTitle()); driver.quit(); }
本博客由亮帮广州办公家具有限公司赞助。