chromedriver的使用

时间:2019-10-03
本文章向大家介绍chromedriver的使用,主要包括chromedriver的使用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1.调用chrome driver

System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");

2.创建chromeOption对象

ChromeOptions chromeOptions = new ChromeOptions();

3.属性设置

3.1无图设置:

Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.managed_default_content_settings.images", 2);
chromeOptions.setExperimentalOption("prefs",prefs);

3.2设置免检测(开发者模式)

List excludeSwitches=new ArrayList<String>();
excludeSwitches.add("enable-automation");
chromeOptions.setExperimentalOption("excludeSwitches",excludeSwitches);

3.3设置代理ip

String ip = "122.236.112.128:4267";
chromeOptions.addArguments("--proxy-server=http://" + ip);

3.4设置浏览器大小

WebDriver driver = new ChromeDriver(chromeOptions);
//调整浏览器大小
driver.manage().window().setSize(new Dimension(1300, 800));
((ChromeDriver) driver).get(url);

3.5设置Cookie

Cookie cookie = new Cookie("name","value");
driver.manage().addCookie(cookie);

3.6设置无头

chromeOptions.addArguments("-headless");

原文地址:https://www.cnblogs.com/ushowtime/p/11619333.html