List of usage examples for org.openqa.selenium Proxy setProxyAutoconfigUrl
public Proxy setProxyAutoconfigUrl(String proxyAutoconfigUrl)
From source file:com.qwazr.crawler.web.driver.BrowserDriverBuilder.java
License:Apache License
public BrowserDriver build() throws ReflectiveOperationException, SecurityException { BrowserDriverEnum browserType = BrowserDriverEnum.html_unit; final WebCrawlDefinition.ProxyDefinition proxyDef; DesiredCapabilities capabilities = null; if (crawlDefinition != null) { // Choose a browser type if (crawlDefinition.browser_type != null) browserType = crawlDefinition.browser_type; proxyDef = selectProxy();//from w ww . ja v a 2 s .co m // Setup the proxy if (proxyDef != null) { capabilities = checkCapabilities(capabilities); org.openqa.selenium.Proxy proxy = new Proxy(); if (proxyDef.http_proxy != null) proxy.setHttpProxy(proxyDef.http_proxy); if (proxyDef.ftp_proxy != null) proxy.setFtpProxy(proxyDef.ftp_proxy); if (proxyDef.ssl_proxy != null) proxy.setSslProxy(proxyDef.ssl_proxy); if (proxyDef.socks_proxy != null) proxy.setSocksProxy(proxyDef.socks_proxy); if (proxyDef.socks_username != null) proxy.setSocksUsername(proxyDef.socks_username); if (proxyDef.socks_password != null) proxy.setSocksPassword(proxyDef.socks_password); if (proxyDef.no_proxy != null) proxy.setNoProxy(proxyDef.no_proxy); if (proxyDef.proxy_autoconfig_url != null) proxy.setProxyAutoconfigUrl(proxyDef.proxy_autoconfig_url); capabilities.setCapability(CapabilityType.PROXY, proxy); } // Setup the language if (crawlDefinition.browser_language != null) { capabilities = checkCapabilities(capabilities); capabilities.setCapability(AdditionalCapabilities.QWAZR_BROWSER_LANGUAGE, crawlDefinition.browser_language); if (browserType == BrowserDriverEnum.phantomjs) capabilities.setCapability( PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX + "Accept-Language", crawlDefinition.browser_language); } // Download images if (crawlDefinition.download_images != null) { capabilities = checkCapabilities(capabilities); if (browserType == BrowserDriverEnum.phantomjs) capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "loadImages", crawlDefinition.download_images); } // Web security if (crawlDefinition.web_security != null) { capabilities = checkCapabilities(capabilities); if (browserType == BrowserDriverEnum.phantomjs) capabilities.setCapability( PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "webSecurityEnabled", crawlDefinition.web_security); } // Choose a browser name if (crawlDefinition.browser_name != null) { capabilities = checkCapabilities(capabilities); capabilities.setBrowserName(crawlDefinition.browser_name); } if (crawlDefinition.browser_version != null) { capabilities = checkCapabilities(capabilities); capabilities.setVersion(crawlDefinition.browser_version); } // Javascript capability if (crawlDefinition.javascript_enabled != null) { capabilities = checkCapabilities(capabilities); capabilities.setJavascriptEnabled(crawlDefinition.javascript_enabled); if (browserType == BrowserDriverEnum.phantomjs) capabilities.setCapability( PhantomJSDriverService.PHANTOMJS_PAGE_SETTINGS_PREFIX + "javascriptEnabled", crawlDefinition.javascript_enabled); } } else proxyDef = null; if (browserType == BrowserDriverEnum.phantomjs) { capabilities = checkCapabilities(capabilities); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] { "--webdriver-loglevel=NONE", "--ignore-ssl-errors=true" }); } final WebDriver driver = browserType.getNewInstance(capabilities); try { final BrowserDriver browserDriver = new BrowserDriver(browserType, driver, proxyDef); browserDriver.setTimeouts(crawlDefinition.implicitly_wait, crawlDefinition.page_load_timeout, crawlDefinition.script_timeout); if (crawlDefinition.cookies != null) for (Map.Entry<String, String> cookie : crawlDefinition.cookies.entrySet()) driver.manage().addCookie(new Cookie(cookie.getKey(), cookie.getValue())); return browserDriver; } catch (Exception e) { driver.quit(); throw e; } }
From source file:org.paxml.selenium.webdriver.NewSessionTag.java
License:Open Source License
private WebDriver createWebDriver(Context context) { Proxy proxy = new Proxy(); if (StringUtils.isNotBlank(proxyHttp)) { proxy.setHttpProxy(proxyHttp);//from w w w. j av a 2 s .c o m } if (StringUtils.isNotBlank(proxyFtp)) { proxy.setFtpProxy(proxyFtp); } if (StringUtils.isNotBlank(proxySsl)) { proxy.setSslProxy(proxySsl); } if (StringUtils.isNotBlank(proxyAutoConfigUrl)) { proxy.setProxyAutoconfigUrl(proxyAutoConfigUrl); } if (proxyAutoDetect != null) { proxy.setAutodetect(proxyAutoDetect); } DesiredCapabilities cap = new DesiredCapabilities(); cap.setCapability(CapabilityType.PROXY, proxy); cap.setJavascriptEnabled(true); if (StringUtils.isNotBlank(hub)) { final URL url; try { url = new URL(hub); } catch (MalformedURLException e) { throw new RuntimeException("hub url is invalid: " + hub, e); } if ("firefox".equalsIgnoreCase(browser)) { cap = cap.firefox(); } else if ("iexplore".equalsIgnoreCase(browser)) { cap = cap.internetExplorer(); } else if ("chrome".equalsIgnoreCase(browser)) { cap = cap.chrome(); } return new RemoteWebDriver(url, cap); } if ("firefox".equalsIgnoreCase(browser)) { return new FirefoxDriver(cap); } if ("iexplore".equalsIgnoreCase(browser)) { return new InternetExplorerDriver(cap); } if ("chrome".equalsIgnoreCase(browser)) { return new ChromeDriver(cap); } throw new RuntimeException("unknown browser type: " + browser); }