注册
登录
 文档中心 产品介绍 开发指南 API接口 代码样例 使用帮助
Java使用selenium调用Http代理IP的代码样例
站大爷 官方 2024-01-20 1643 浏览

温馨提示:

1.  基于终端IP授权模式使用selenium-java认证代理

2.  下载chromedriver(注意chromedriver版本要和Chrome版本对应)

3.  依赖下载:selenium-java-4.1.2.jar


import org.openqa.selenium.By;
import org.openqa.selenium.Proxy;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;


public class TestProxySelenium {
	public static void main(String[] args) throws InterruptedException {
	// 目标网站
		String targetUrl = "https://example.com";

		// 代理ip: port
		String proxyServer = "159.138.141.125:18916";
		// 创建webdriver驱动,设置代理
		System.setProperty("webdriver.chrome.driver", "${chromedriver_path}"); // webdriver驱动路径
		Proxy proxy = new Proxy().setHttpProxy(proxyServer).setSslProxy(proxyServer);
		ChromeOptions options = new ChromeOptions();
		options.setProxy(proxy);
		WebDriver driver = new ChromeDriver(options);

		// 发起请求
		driver.get(targetUrl);
		WebElement element = driver.findElement(By.xpath("/html"));
		String resText = element.getText().toString();
		System.out.println(resText);
		Thread.sleep(3000);

		// 关闭webdriver
		driver.quit();
	}
}


立即注册站大爷用户,免费试用全部产品
立即注册站大爷用户,免费试用全部产品