一、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;
}
}