Selenium,ChromeDriver之Chrome浏览器设置,打印控制台日志(Network,console等)-番外篇

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

chrome浏览器与chromedriver版本对应

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

六、打印控制台日志实例
 

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

 

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 以下是seleniumchromedriverchrome版本映射表: | chromedriver版本 | 支持的Chrome版本 | | ---------------- | ---------------- | | v2.46 | v71-73 | | v2.45 | v70-72 | | v2.44 | v69-71 | | v2.43 | v69-71 | | v2.42 | v68-70 | | v2.41 | v67-69 | | v2.40 | v66-68 | | v2.39 | v66-68 | | v2.38 | v65-67 | | v2.37 | v64-66 | | v2.36 | v63-65 | | v2.35 | v62-64 | | v2.34 | v61-63 | | v2.33 | v60-62 | | v2.32 | v59-61 | | v2.31 | v58-60 | | v2.30 | v58-60 | | v2.29 | v56-58 | | v2.28 | v55-57 | | v2.27 | v54-56 | | v2.26 | v53-55 | | v2.25 | v53-55 | | v2.24 | v52-54 | | v2.23 | v51-53 | | v2.22 | v49-52 | | v2.21 | v46-50 | | v2.20 | v43-48 | | v2.19 | v43-47 | | v2.18 | v43-46 | | v2.17 | v42-43 | | v2.13 | v42 | 注意:以上版本仅供参考,具体版本兼容性请参考selenium官方文档。 ### 回答2: selenium是一个自动化测试工具,可以用于测试网站的功能和兼容性。而chromedriver则是selenium中用于控制和操作Chrome浏览器的驱动程序。 chromedriverchrome浏览器的版本是有一定的对应关系的,不同版本的chromedriver适用于不同版本的chrome浏览器。具体的版本映射表如下: - chromedriver v2.46适用于Chrome v71-73。 - chromedriver v74适用于Chrome v74-76。 - chromedriver v75适用于Chrome v75-77。 - chromedriver v76适用于Chrome v76-78。 - chromedriver v77适用于Chrome v77-79。 - chromedriver v78适用于Chrome v78-80。 - chromedriver v79适用于Chrome v79-81。 - chromedriver v80适用于Chrome v80-83。 - chromedriver v81适用于Chrome v81-83。 - chromedriver v83适用于Chrome v83-85。 - chromedriver v84适用于Chrome v84-85。 - chromedriver v85适用于Chrome v85-87。 需要注意的是,这只是其中的一部分版本映射表,因为seleniumchrome都会不断更新,所以版本对应关系可能会有所变动。所以,在使用seleniumchromedriver时,需要根据使用的具体版本来选择合适的chromedriver。为了确保兼容性和稳定性,建议先查看selenium官方文档或者chromedriver官方文档,或者在开发者社区中寻找最新的版本映射信息。 ### 回答3: selenium是一种用于自动化测试的工具,它可以模拟用户在浏览器上的操作,进行网页内容的抓取和功能的测试。 在使用selenium进行自动化测试时,需要下载对应的浏览器驱动程序,比如Chrome浏览器需要使用chromedriverchromedriver是一个连接Chrome浏览器selenium的桥梁,它通过与Chrome进行通信来控制浏览器的行为。不同版本的Chrome浏览器对应不同版本的chromedriver,所以在使用selenium时需要注意浏览器和驱动的版本兼容性。 以下是常见的Chrome浏览器版本和chromedriver的版本映射表: Chrome版本 对应chromedriver版本 Chrome v88 chromedriver v88 Chrome v87 chromedriver v87 Chrome v86 chromedriver v86 Chrome v85 chromedriver v85 Chrome v84 chromedriver v84 需要注意的是,Chrome浏览器会经常更新版本,所以推荐在使用selenium时使用最新版本的Chrome浏览器chromedriver,以确保兼容性和性能。可以通过官方网站或者开发者社区获取最新的chromedriver版本,并按照说明进行下载和配置。 总之,使用selenium的时候需要根据自己使用的Chrome浏览器版本选择对应的chromedriver版本,以确保正常的自动化测试操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值