配置 Selenium 驱动程序以使用 Socks5 代理

configuring Selenium driver to use Socks5 proxy

我在使用 Selenium 网络驱动程序时尝试使用我的 Socks5 代理,这是我的代码:

        System.setProperty("webdriver.chrome.driver", "dependencies/chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        Proxy prx =new Proxy() ;
        prx.setSocksVersion(5);
        prx.setSocksProxy("xx.xxx.xxx.xxx:8000");
        prx.setSocksPassword("5xxxxS") ;
        prx.setSocksUsername("VxxxxW") ;
        options.addArguments("--disable-notifications");
        options.addArguments("start-minimized");
        options.setCapability("proxy", prx);
        WebDriver driver = new ChromeDriver(options); // issue here

我遇到以下错误:

Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.

您应该将代理服务器参数添加到 ChromeOptions:

String host = "shadowsocks6.freeproxy.center";
String port = "8989";
String socks5User = "SOCKS_USER";
String socks5Pass = "SOCKS_Pass";

ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--proxy-server=socks5://" + host + ":" + port);
chromeOptions.addArguments("--proxy-auth=" + socks5User + ":" + socks5Pass);
WebDriver driver =  new ChromeDriver(chromeOptions);

完整源码可以参考selenium-tiny-projects