• PhantomJS(Python)
  • Chrome(Python)
  • HtmlUnitDriver(Java)
  • FirefoxDriver(Java)
  • PhantomJS(Java)
  • Chrome(Java)

    #coding=utf-8
    from selenium import webdriver

    # 代理服务器
    proxyHost = "http-dynamic.xiaoxiangdaili.com"
    proxyPort = "10030"

    # 代理隧道验证信息
    proxyUser = "应用id(后台-产品管理-隧道代理页面可查)"
    proxyPass = "应用密码(后台-产品管理-隧道代理页面可查)"

    service_args = [
        "--proxy-type=http",
        "--proxy=%(host)s:%(port)s" % {
            "host" : proxyHost,
            "port" : proxyPort,
        },
        "--proxy-auth=%(user)s:%(pass)s" % {
            "user" : proxyUser,
            "pass" : proxyPass,
        },
    ]

    # 要访问的目标页面
    targetUrl = "http://httpbin.org/ip"

    phantomjs_path = r"./phantomjs"

    driver = webdriver.PhantomJS(executable_path=phantomjs_path, service_args=service_args)
    driver.get(targetUrl)

    print driver.title
    print driver.page_source.encode("utf-8")

    driver.quit()

    
    from selenium import webdriver
    import string
    import zipfile
    
    # 代理服务器
    proxyHost = "http-dynamic.xiaoxiangdaili.com"
    proxyPort = "10030"
    
    # 代理隧道验证信息
    proxyUser = "应用id(后台-产品管理-隧道代理页面可查)"
    proxyPass = "应用密码(后台-产品管理-隧道代理页面可查)"
    
    
    def create_proxy_auth_extension(proxy_host, proxy_port,
    							proxy_username, proxy_password,
    							scheme='http', plugin_path=None):
    	if plugin_path is None:
    		plugin_path = r'./proxy_auth_plugin.zip'
    
    	manifest_json = """
    		{
    			"version": "1.0.0",
    			"manifest_version": 2,
    			"name": "Abuyun Proxy",
    			"permissions": [
    				"proxy",
    				"tabs",
    				"unlimitedStorage",
    				"storage",
    				"<all_urls>",
    				"webRequest",
    				"webRequestBlocking"
    			],
    			"background": {
    				"scripts": ["background.js"]
    			},
    			"minimum_chrome_version":"22.0.0"
    		}
    		"""
    
    	background_js = string.Template(
    		"""
    		var config = {
    			mode: "fixed_servers",
    			rules: {
    				singleProxy: {
    					scheme: "${scheme}",
    					host: "${host}",
    					port: parseInt(${port})
    				},
    				bypassList: ["foobar.com"]
    			}
    		};
    
    		chrome.proxy.settings.set({value: config, scope: "regular"}, function() {});
    
    		function callbackFn(details) {
    			return {
    				authCredentials: {
    					username: "${username}",
    					password: "${password}"
    				}
    			};
    		}
    
    		chrome.webRequest.onAuthRequired.addListener(
    			callbackFn,
    			{urls: ["<all_urls>"]},
    			['blocking']
    		);
    		"""
    	).substitute(
    		host=proxy_host,
    		port=proxy_port,
    		username=proxy_username,
    		password=proxy_password,
    		scheme=scheme,
    	)
    
    	with zipfile.ZipFile(plugin_path, 'w') as zp:
    		zp.writestr("manifest.json", manifest_json)
    		zp.writestr("background.js", background_js)
    
    	return plugin_path
    
    
    proxy_auth_plugin_path = create_proxy_auth_extension(
    proxy_host=proxyHost,
    proxy_port=proxyPort,
    proxy_username=proxyUser,
    proxy_password=proxyPass)
    
    option = webdriver.ChromeOptions()
    
    option.add_argument("--start-maximized")
    option.add_extension(proxy_auth_plugin_path)
    
    driver = webdriver.Chrome(chrome_options=option)
    
    driver.get("http://httpbin.org/ip")
    

    import org.json.JSONException;
    import org.json.JSONObject;
    import org.openqa.selenium.Platform;
    import org.openqa.selenium.Proxy;
    import org.openqa.selenium.htmlunit.HtmlUnitDriver;
    import org.openqa.selenium.remote.CapabilityType;
    import org.openqa.selenium.remote.DesiredCapabilities;

    import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
    import com.gargoylesoftware.htmlunit.WebClient;

    public class HtmlUnitDriverProxyDemo
    {
        // 代理隧道验证信息
        final static String proxyUser = "应用id(后台-产品管理-隧道代理页面可查)";
        final static String proxyPass = "应用密码(后台-产品管理-隧道代理页面可查)";

        // 代理服务器
        final static String proxyServer = "http-dynamic.xiaoxiangdaili.com:10030";

        public static void main(String[] args) throws JSONException
        {
            HtmlUnitDriver driver = getHtmlUnitDriver();

            driver.get("http://httpbin.org/ip");

            String title = driver.getTitle();
            System.out.println(title);
        }

        public static HtmlUnitDriver getHtmlUnitDriver()
        {
            HtmlUnitDriver driver = null;

            Proxy proxy = new Proxy();
            // 设置代理服务器地址
            proxy.setHttpProxy(proxyServer);

            DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
            capabilities.setCapability(CapabilityType.PROXY, proxy);
            capabilities.setJavascriptEnabled(true);
            capabilities.setPlatform(Platform.WIN8_1);

            driver = new HtmlUnitDriver(capabilities) {
                @Override
                protected WebClient modifyWebClient(WebClient client) {
                    DefaultCredentialsProvider creds = new DefaultCredentialsProvider();
                    creds.addCredentials(proxyUser, proxyPass);
                    client.setCredentialsProvider(creds);
                    return client;
                }
            };

            driver.setJavascriptEnabled(true);

            return driver;
        }
    }


    import org.json.JSONException;
    import org.json.JSONObject;
    import org.openqa.selenium.Platform;
    import org.openqa.selenium.Proxy;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.firefox.FirefoxProfile;
    import org.openqa.selenium.htmlunit.HtmlUnitDriver;
    import org.openqa.selenium.remote.CapabilityType;
    import org.openqa.selenium.remote.DesiredCapabilities;

    import com.gargoylesoftware.htmlunit.DefaultCredentialsProvider;
    import com.gargoylesoftware.htmlunit.WebClient;

    public class FirefoxDriverProxyDemo
    {
        // 代理隧道验证信息
        final static String proxyUser = "应用id(后台-产品管理-隧道代理页面可查)";
        final static String proxyPass = "应用密码(后台-产品管理-隧道代理页面可查)";

        // 代理服务器
        final static String proxyHost = "http-dynamic.xiaoxiangdaili.com";
        final static int proxyPort = 10030;

        final static String firefoxBin = "D:/Program Files/Mozilla Firefox/firefox.exe";

        public static void main(String[] args) throws JSONException
        {
            System.setProperty("webdriver.firefox.bin", firefoxBin);

            FirefoxProfile profile = new FirefoxProfile();
            // 使用代理
            profile.setPreference("network.proxy.type", 1);
            // 代理服务器配置
            profile.setPreference("network.proxy.http", proxyHost);
            profile.setPreference("network.proxy.http_port", proxyPort);

            profile.setPreference("network.proxy.ssl", proxyHost);
            profile.setPreference("network.proxy.ssl_port", proxyPort);

            profile.setPreference("username", proxyUser);
            profile.setPreference("password", proxyPass);

            // 所有协议公用一种代理配置,如果单独配置,这项设置为false
            profile.setPreference("network.proxy.share_proxy_settings", true);

            // 对于localhost的不用代理,这里必须要配置,否则无法和webdriver通讯
            profile.setPreference("network.proxy.no_proxies_on", "localhost");

            // 以代理方式启动firefox
            FirefoxDriver driver = new FirefoxDriver(profile);
        }
    }


    import org.apache.commons.lang.StringUtils;
    import org.openqa.selenium.Proxy;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.phantomjs.PhantomJSDriver;
    import org.openqa.selenium.phantomjs.PhantomJSDriverService;
    import org.openqa.selenium.remote.DesiredCapabilities;

    public class PhantomJSDriverDome
    {
        public static String proxyHost = "http-dynamic.xiaoxiangdaili.com";
        public static int proxyPort = 10030;

        public static String proxyUser = "应用id(后台-产品管理-隧道代理页面可查)";
        public static String proxyPass = "应用密码(后台-产品管理-隧道代理页面可查)";

        // 本机phantomjs的存储路径
        public static String PHANTOMJS_BINARY_PATH = "E:/phantomjs-2.1.1-windows/bin/phantomjs.exe";

        public static WebDriver getPhantomjsDriver()
        {
            return getPhantomjsDriver("", 0);
        }

        public static WebDriver getPhantomjsDriver(String proxyIp, int proxyPort)
        {
            System.setProperty("phantomjs.binary.path", PHANTOMJS_BINARY_PATH);

            WebDriver driver = null;
            DesiredCapabilities cap = DesiredCapabilities.phantomjs();

            if ((StringUtils.isNotBlank(proxyIp)) && (proxyPort != 0))
            {
                String proxyIpAndPort = proxyIp + ":" + proxyPort;

                Proxy proxy = new Proxy();
                proxy.setHttpProxy(proxyIpAndPort).setFtpProxy(proxyIpAndPort).setSslProxy(proxyIpAndPort);

                cap.setCapability("avoidProxy", true);
                cap.setCapability("onlyProxySeleniumTraffic", true);

                System.setProperty("http.nonProxyHosts", "localhost");

                cap.setCapability("proxy", proxy);
            }

            driver = new PhantomJSDriver(cap);

            driver.manage().timeouts().implicitlyWait(5L, TimeUnit.SECONDS);
            driver.manage().window().maximize();

            return driver;
        }

        public static WebDriver getPhantomjsDriver(String address, String username, String password)
        {
            System.setProperty("phantomjs.binary.path", PHANTOMJS_BINARY_PATH);

            WebDriver driver = null;
            ArrayList cliArgsCap = new ArrayList();

            cliArgsCap.add("--proxy="+address);
            cliArgsCap.add("--proxy-auth=" + username + ":" + password);
            cliArgsCap.add("--proxy-type=http");

            DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();

            capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgsCap);

            driver = new PhantomJSDriver(capabilities);
            driver.manage().timeouts().implicitlyWait(5L, TimeUnit.SECONDS);
            driver.manage().window().maximize();

            return driver;
        }

        public static void main(String[] args)
        {
            WebDriver driver = getPhantomjsDriver(proxyHost, proxyUser, proxyPass);

            driver.get("http://test.abuyun.com");

            System.out.println(driver.getPageSource());
        }
    }

    package com.xiangzhi.xxproxy.linemanager;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.remote.RemoteWebDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    import java.io.*;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.zip.CRC32;
    import java.util.zip.CheckedOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipOutputStream;
    
    public class ChromeDriverDemo {
    
        final static String pluginPath = "C:\\Users\\admin\\Desktop\\proxy.zip";
        final static String driverPath = "C:\\Users\\admin\\Desktop\\chromedriver.exe";
    
        final static String proxyUser = "应用id(后台-产品管理-隧道代理页面可查)";
        final static String proxyPass = "应用密码(后台-产品管理-隧道代理页面可查)";
    
        // 代理服务器
        final static String proxyHost = "http-dynamic.xiaoxiangdaili.com";
        final static int proxyPort = 10030;
    
        static {
            String manifestJson = "{\n" +
                    "    \"version\": \"1.0.0\",\n" +
                    "    \"manifest_version\": 2,\n" +
                    "    \"name\": \"Chrome Proxy\",\n" +
                    "    \"permissions\": [\n" +
                    "        \"proxy\",\n" +
                    "        \"tabs\",\n" +
                    "        \"unlimitedStorage\",\n" +
                    "        \"storage\",\n" +
                    "        \"\",\n" +
                    "        \"webRequest\",\n" +
                    "        \"webRequestBlocking\"\n" +
                    "    ],\n" +
                    "    \"background\": {\n" +
                    "        \"scripts\": [\"background.js\"]\n" +
                    "    },\n" +
                    "    \"minimum_chrome_version\":\"22.0.0\"\n" +
                    "}";
    
            String backgroundJs = "var config = {\n" +
                    "        mode: \"fixed_servers\",\n" +
                    "        rules: {\n" +
                    "          singleProxy: {\n" +
                    "            scheme: \"http\",\n" +
                    "            host: \"" + proxyHost + "\",\n" +
                    "            port: " + proxyPort + "\n" +
                    "          },\n" +
                    "          bypassList: [\"www.xiaoxiangdaili.com\"]\n" +
                    "        }\n" +
                    "      };\n" +
                    "\n" +
                    "chrome.proxy.settings.set({value: config, scope: \"regular\"}, function() {});\n" +
                    "\n" +
                    "function callbackFn(details) {\n" +
                    "    return {\n" +
                    "        authCredentials: {\n" +
                    "            username: \"" + proxyUser + "\",\n" +
                    "            password: \"" + proxyPass + "\"\n" +
                    "        }\n" +
                    "    };\n" +
                    "}\n" +
                    "\n" +
                    "chrome.webRequest.onAuthRequired.addListener(\n" +
                    "            callbackFn,\n" +
                    "            {urls: [\"\"]},\n" +
                    "            ['blocking']\n" +
                    ");";
    
            HashMap hashMap = new HashMap<>();
            hashMap.put("manifest.json", manifestJson);
            hashMap.put("background.js", backgroundJs);
            zipFile(hashMap);
        }
    
        public static void main(String[] args) throws Exception {
    
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.addArguments("--start-maximized");
            chromeOptions.addExtensions(new File(pluginPath));      //将proxy的信息添加到ChromeOptions中
            System.setProperty("webdriver.chrome.driver", driverPath); //配置chromedriver.exe的路径信息
            RemoteWebDriver webdriver = new ChromeDriver(chromeOptions);
            webdriver.get("https://www.baidu.com/");
            WebElement webElement = new WebDriverWait(webdriver, 5).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input#kw")));
            System.out.println(webElement.getAttribute("class"));
            //获取浏览器当前网页标题与地址
            System.out.println(webdriver.getTitle());
            System.out.println(webdriver.getCurrentUrl());
        }
    
        public static void zipFile(Map map) {
            try {
                File file = new File(pluginPath);
                if (file.exists()){
                    return;
                }
                ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file));
                for (Map.Entry entry : map.entrySet()) {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    CheckedOutputStream cos = new CheckedOutputStream(bos, new CRC32());
                    transfer(new ByteArrayInputStream(entry.getValue().getBytes()), cos);
                    ZipEntry zipEntry = new ZipEntry(entry.getKey());
                    zipEntry.setTime(System.currentTimeMillis());
                    zipEntry.setMethod(ZipEntry.STORED);
                    zipEntry.setSize(bos.size());
                    zipEntry.setCrc(cos.getChecksum().getValue());
                    zos.putNextEntry(zipEntry);
                    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
                    transfer(bis, zos);
                    zos.closeEntry();
                }
                zos.finish();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    
        public static long transfer(InputStream in, OutputStream out) throws IOException {
            long total = 0;
            byte[] buffer = new byte[4096];
            int length;
            while ((length = in.read(buffer)) != -1) {
                out.write(buffer, 0, length);
                total += length;
            }
            out.flush();
            return total;
        }
    }