无法使用代理 Java 连接到使用 Selenium 的站点,代理服务器处于活动状态,但它无法与我的程序一起工作。
我的代码:
public void start(){
String proxyAddress = "130.185.126.159";
int proxyPort = 6774;
String proxyUsername = "lznbghpe";
String proxyPassword = "16z9hv6q9fhu";
Proxy proxy = new Proxy();
proxy.setHttpProxy(proxyAddress + ":" + proxyPort);
proxy.setSslProxy(proxyAddress + ":" + proxyPort);
String proxyAuth = proxyUsername + ":" + proxyPassword;
proxy.setProxyType(Proxy.ProxyType.MANUAL);
proxy.setHttpProxy(proxyAuth + "@" + proxyAddress + ":" + proxyPort);
proxy.setSslProxy(proxyAuth + "@" + proxyAddress + ":" + proxyPort);
ChromeOptions options = new ChromeOptions();
options.setProxy(proxy);
options.addArguments("--headless"); // Run in headless mode
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);
driver.get("https://mycgsportal.com/MyCGS/CGSAccount/Login");
System.out.println(driver.getTitle());
}
回应:
Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: net::ERR_CONNECTION_TIMED_OUT
我也遇到过类似的情况。但对我来说,我希望 Selenium 使用我的操作系统中配置的 HTTP 代理。也许这也适合你。 我可以通过
ProxyType.AUTODETECT
做到这一点
// Create a Proxy object and let it use the OS proxy settings
Proxy proxy = new Proxy();
proxy.setProxyType(Proxy.ProxyType.AUTODETECT);
// Create ChromeOptions instance and set the proxy options
ChromeOptions options = new ChromeOptions();
options.setProxy(proxy);
使用 selenium-java 4.18.0 进行测试