大数据服务和电子商务供应服务商

安全+稳定+完美售后

免费咨询热线:15366059791

Selenium 接入长效ip代理指南-HtmlUnitDriver(Java)篇


我们很多朋友遇到使用云速互联的长效ip代理产品接入到Selenium 程序中,以下是接入长效ip部分代码:

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 = "www.socks5daili.com";
        final static String proxyPass = "www.socks5daili.com";

        // 代理服务器
        final static String proxyHost = "www.socks5daili.com";
        final static int proxyPort = 65333;

        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);
        }
    }


您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。