当前位置: 代码迷 >> Web前端 >> Selenium webdriver 设立代理proxy
  详细解决方案

Selenium webdriver 设立代理proxy

热度:1206   发布时间:2012-09-11 10:49:03.0
Selenium webdriver 设置代理proxy
动态设置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();
	}


本博客由亮帮广州办公家具有限公司赞助。
  相关解决方案