赞
踩
chrome地址栏中输入 chrome://chrome-urls/
详情如下
检查版本信息,浏览器基本信息
chrome://version/
参考地址https://peter.sh/experiments/chromium-command-line-switches/
一些常用配置:
//消除安全校验 可以直接无提示访问http网站
--allow-running-insecure-content
//默认最大化启动
--start-maximized
//关闭gpu
--disable-gpu
//无界面模式启动
--headless
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --allow-running-insecure-content --start-maximized
如图,在chrome图标->右键->属性->目标
http://chromedriver.storage.googleapis.com/index.html
各个chrome浏览器和chromedriver版本对应关系,可以在连接中找到任意一个版本点击进去,查看notes.txt,如:
http://chromedriver.storage.googleapis.com/2.33/notes.txt
- /**
- * 通过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;
- }
- }
- package cn.ms22.driver;
-
- import org.openqa.selenium.WebDriver;
- import org.openqa.selenium.chrome.ChromeDriver;
- import org.openqa.selenium.chrome.ChromeOptions;
- import org.openqa.selenium.logging.LogType;
- import org.openqa.selenium.logging.LoggingPreferences;
- import org.openqa.selenium.remote.CapabilityType;
- import org.openqa.selenium.remote.DesiredCapabilities;
-
- import java.util.HashMap;
- import java.util.concurrent.TimeUnit;
- import java.util.logging.Level;
-
- /**
- * 通过chromeDriver 启动chrome浏览器
- * 通过设置日志,查看network控制台日志
- * @author baopz
- * @date 2019.02.21
- */
- public class DriverApplication {
-
- private static final String url = "https://www.baidu.com";
-
- public static void main(String[] args) {
- //设置驱动所在位置
- System.setProperty("webdriver.chrome.driver", "C:\\Users\\Administrator\\Desktop\\driver\\chromedriver.exe");
-
- WebDriver driver = new ChromeDriver(initChromeOpts());
- driver.get(url);
-
- //查看支持的日志类型
- for (String availableLogType : driver.manage().logs().getAvailableLogTypes()) {
- System.out.println(availableLogType);
- }
-
- //打印PERFORMANCE日志
- driver.manage().logs().get(LogType.PERFORMANCE).iterator().forEachRemaining(logEntry -> {
- System.out.println(logEntry.getMessage());
- });
-
- //做一些事
- try {
- TimeUnit.SECONDS.sleep(5);
- } catch (InterruptedException e) {
- e.printStackTrace();
- } finally {
- //关闭浏览器,并退出chromeDirver
- driver.quit();
- //只关闭浏览器,chromeDriver不会关闭,依然在后台运行
- // driver.close();
- }
- }
-
- /**
- * 设置浏览器所需参数
- *
- * @return
- */
- private static DesiredCapabilities initChromeOpts() {
- //这里可以不设置浏览器所在位置,这样系统会寻找所需浏览器,如果没有找到,抛错
- ChromeOptions chromeOptions = new ChromeOptions();
- chromeOptions.setBinary("C:\\Users\\Administrator\\AppData\\Local\\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");
- //打开控制台
- //chromeOptions.addArguments("--auto-open-devtools-for-tabs");
-
- //其他,日志设置相关
- DesiredCapabilities caps = DesiredCapabilities.chrome();
- LoggingPreferences loggingPreferences = new LoggingPreferences();
- loggingPreferences.enable(LogType.PERFORMANCE, Level.ALL);
- loggingPreferences.enable(LogType.BROWSER, Level.ALL);
-
- caps.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
- caps.setCapability(CapabilityType.LOGGING_PREFS, loggingPreferences);
- return caps;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。