一、Chrome信息检测,chrome://chrome-urls/
chrome地址栏中输入 chrome://chrome-urls/
详情如下
检查版本信息,浏览器基本信息
chrome://version/
二、Chrome启动参数
参考地址https://peter.sh/experiments/chromium-command-line-switches/
一些常用配置:
//消除安全校验 可以直接无提示访问http网站
--allow-running-insecure-content
//默认最大化启动
--start-maximized
//关闭gpu
--disable-gpu
//无界面模式启动
--headless
三、chrome设置参数启动,可带多个参数
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-running-insecure-content --start-maximized
如图,在chrome图标->右键->属性->目标
四、chromeDriver下载
http://chromedriver.storage.googleapis.com/index.html
各个chrome浏览器和chromedriver版本对应关系,可以在连接中找到任意一个版本点击进去,查看notes.txt,如:
http://chromedriver.storage.googleapis.com/2.33/notes.txt
五、chromeDriver 添加扩充,initChromeOpts()方法
/**
* 通过Selenuim启动chrome浏览器
* @author Baopz
* @date 2018/05/24
*/
public class SeleniumApplication {
private static final String base = "https://www.baidu.com";
public static void main(String[] args) {
//设置驱动所在位置
System.setProperty("webdriver.chrome.driver","C:\\Users\\Baopz\\Desktop\\dcm\\2.37\\chromedriver.exe");
WebDriver driver = new ChromeDriver(initChromeOpts());
driver.get(base);
//做一些事
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
//关闭浏览器
driver.quit();
}
/**
* 设置浏览器所需参数
* @return
*/
private static ChromeOptions initChromeOpts() {
ChromeOptions chromeOptions = new ChromeOptions();
//这里可以不设置浏览器所在位置,这样系统会寻找所需浏览器,如果没有找到,抛错
chromeOptions.setBinary("C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe");
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
//禁止弹窗
chromePrefs.put("profile.default_content_settings.popups", 0);
//下载地址
chromePrefs.put("download.default_directory", "C://xx//");
//禁止图片加载
chromePrefs.put("profile.managed_default_content_settings.images", 2);
//userAgent=ie11
String userAgentIE11="Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.117 Safari/537.36";
chromePrefs.put("profile.general_useragent_override", userAgentIE11);
HashMap<String, Object> mobileEmulation = new HashMap<String, Object>();
//用iPhone X 屏幕启动
mobileEmulation.put("deviceName","iPhone X");
chromeOptions.setExperimentalOption("prefs",chromePrefs);
chromeOptions.setExperimentalOption("mobileEmulation",mobileEmulation);
/***********************************以下设置启动参数******************************************/
//消除安全校验
chromeOptions.addArguments("--allow-running-insecure-content");
//启动最大化,防止失去焦点
chromeOptions.addArguments("--start-maximized");
//关闭gpu图片渲染
chromeOptions.addArguments("--disable-gpu");
return chromeOptions;
}
}