[WebDriver]Windows安装chromedriver

mac系统安装chromedriver请参考:
[WebDriver]Mac安装chromedriver

Windows系统安装chromedriver:

1.从http://chromedriver.storage.googleapis.com/index.html下载chromedriver包,注意chromedriver和chrome浏览器的版本对应关系。

2.将chromedriver解压放到chrome的安装目录下


3.设置环境变量
选择系统变量Path,点击编辑:



将chromedriver的目录设置到Path中:


设置完环境变量,命令chromedriver -v验证下是否设置成功,如果设置成功,回显中会显示版本信息:

4.写个demo代码吧:

public static void main(String[] args) {
        ChromeDriver client = null;
        try {
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--headless");
            options.addArguments("--disable-gpu");

            client = new ChromeDriver (options);
            client.manage().deleteAllCookies();
            client.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            Dimension dimension = new Dimension(1400, 900);
            client.manage().window().setSize(dimension);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
}

执行会报错:
The path to the driver executable must be set by the webdriver.chrome.driver system property; for more information, see https://github.com/SeleniumHQ/selenium/wiki/ChromeDriver. The latest version can be downloaded from http://chromedriver.storage.googleapis.com/index.html

需要在new ChromeDriver()前加段代码,设置webdriver.chrome.driver系统变量,这样就可以成功启动ChromeDriver实例了。

public static void main(String[] args) {
        ChromeDriver client = null;
        // 设置系统变量:
        System.setProperty("webdriver.chrome.driver","C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
        try {
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--headless");
            options.addArguments("--disable-gpu");

            client = new ChromeDriver (options);
            client.manage().deleteAllCookies();
            client.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            Dimension dimension = new Dimension(1400, 900);
            client.manage().window().setSize(dimension);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。