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");

 

本文来源于 ushowtime https://www.ushowtime.cn

posted @ 2019-10-03 11:01  执笔coding  阅读(5577)  评论(0编辑  收藏  举报