前言
之前为公司写了个爬虫项目,有时候为了绕过网站的反爬机制需要用到ip代理以及设置浏览器为开发者模式,看了一些selenium官方文档,没发现有具体设置的代码,在网上查了很久发现很少有整理这块代码的文章,写python+selenium的就很多,但是python的selenium中设置这两个东西的代码跟在java中区别挺大的,摸索了半天才知道该怎么写。这里整理下selenium常用的几个操作,之后有机会再更新。
代码
String chrome_driver_path = “” // 驱动路径
System.setProperty("webdriver.chrome.driver", chrome_driver_path);
String httpUrl = “127.0.0.1:8080”; // 代理IP示例
// 设置代理IP
options.addArguments("--proxy-server=http://" + httpUrl);
ChromeOptions options = new ChromeOptions();
// 开启开发者模式
options.setExperimentalOption("excludeSwitches", Collections.singletonList("enable-automation"));
WebDriver driver = new ChromeDriver(options);
// 窗口最大化
driver.manage().window().maximize();
driver.get(“https://www.csdn.net");