selenium操作chrome浏览器需要有ChromeDriver驱动来协助。
一、什么是ChromeDriver?
ChromeDriver是Chromium team开发维护的,它是实现WebDriver有线协议的一个单独的服务。ChromeDriver通过chrome的自动代理框架控制浏览器,ChromeDriver只与12.0.712.0以上版本的chrome浏览器兼容。

二、启动chrome浏览器
那么要想selenium成功的操作chrome浏览器需要经历如下步骤:
1、下载ChromeDriver驱动包(下载地址:http://chromedriver.storage.googleapis.com/index.html?path=2.7/

注意阅读note.txt下载与自己所使用浏览器一致版本的驱动包。
2、指定ChromeDriver所在位置,可以通过两种方法指定:
1)通过配置ChromeDriver.exe位置到path环境变量实现。
2)通过webdriver.chrome.driver.系统属性实现。实现代码如下:
System.setProperty("webdriver.chrome.driver", "C:\Documents and Settings\Administrator\Local Settings\Application Data\Google\Chrome\Application\chromedriver.exe");
3、最后需要做的就是创建一个新的ChromeDriver的实例。
WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com/");
至此我们就可以通过chrome浏览器来执行我们的自动化代码了。
完整实例代码如下:

public static void main(String[] args) {

// TODO Auto-generated method stub

//设置访问ChromeDriver的路径

System.setProperty("webdriver.chrome.driver", "C:\Documents and Settings\Administrator\LocalSettings\Application Data\Google\Chrome\Application\chromedriver.exe");

WebDriver driver = new ChromeDriver();

driver.get("http://www.baidu.com/");

}
btw:
chrome浏览器在各个系统默认位置:
OS Expected Location of Chrome
Linux /usr/bin/google-chrome1
Mac /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
Windows XP %HOMEPATH%Local SettingsApplication DataGoogleChromeApplicationchrome.exe
Windows Vista C:Users\%USERNAME%AppDataLocalGoogleChromeApplicationchrome.exe

三、chromedriverService

执行以上代码你会发现ChromeDriver仅是在创建是启动,调用quit时关闭浏览器,ChromeDriver是轻量级的服务若在一个比较大的测试套件中频繁的启动关闭,会增加一个比较明显的延时导致浏览器进程不被关闭的情况发生,为了避免这一状况我们可以通过ChromeDriverService来控制ChromeDriver进程的生死,达到用完就关闭的效果避免进程占用情况出现(Running the server in a child process)。
具体实现如下:
ChromeDriverService service = new ChromeDriverService.Builder() .usingChromeDriverExecutable(new File("E:\Selenium WebDriver\chromedriver_win_23.0.1240.0\chromedriver.exe")).usingAnyFreePort().build();
service.start();
driver = new ChromeDriver();
driver.get("http://www.baidu.com");
driver.quit();
// 关闭 ChromeDriver 接口
service.stop();

【转自】http://www.spasvo.com/ceshi/open/kygncsgj/Selenium/20131213102816.html

【Selenium专题】WebDriver启动Chrome浏览器(一)的更多相关文章

  1. selenium webdriver启动Chrome浏览器后无法输入网址的解决办法

    通过selenium webdriver启动Chrome浏览器,脚本如下: from selenium import webdriver browser = webdriver.Chrome() br ...

  2. 【Selenium专题】WebDriver启动Chrome浏览器(二)

    官方API Constructor Summary ChromeDriver() Creates a new ChromeDriver using the default server configu ...

  3. 设置Webdriver启动chrome为默认用户的配置信息

    Webdriver 启动Chrome浏览器时,默认是打开一个新用户,而非默认用户.即新用户没有我们安装扩展程序.但在实际应用中,我们会须要 默认用户安装的一些扩展程序,比方对于某些js或者css样式. ...

  4. selenium webdriver 启动三大浏览器Firefox,Chrome,IE

    selenium webdriver 启动三大浏览器Firefox,Chrome,IE 1.安装selenium 在联网的情况下,在Windows命令行(cmd)输入pip install selen ...

  5. Selenium启动Chrome浏览器提示“请停用以开发者模式运行的扩展程序”的解决办法

    安装了python selenium,运行下面代码: 1 from selenium import webdriver 2 3 browser = webdriver.Chrome() 4 brows ...

  6. selenium webdriver启动IE浏览器失败的解决办法

    通过selenium webdriver启动IE浏览器失败,报错:selenium.common.exceptions.WebDriverException: Message: Unexpected ...

  7. Java&Selenium根据实参启动相应浏览器

    Java&Selenium根据实参启动相应浏览器 /** * 定义函数initBrowser * @param browser:字符串参数chrome/ie/xx * @return 并返回驱 ...

  8. 启动Chrome浏览器弹出“You are using an unsupported command-line flag –ignore-certificate-errors. Stability and security will suffer”

    采用如下代码: public static void launchChrome() { System.setProperty("webdriver.chrome.driver", ...

  9. ChromeDriver启动Chrome浏览器后,地址栏只显示data;——chromeDriver版本不对

    ChromeDriver启动Chrome浏览器后,地址栏只显示data; 错误原因: chromeDriver版本不对,不同版本的chromeDriver对应不同版本的chrome浏览器 chrome ...

随机推荐

  1. debian下使用dig/nslookup

    debian默认没有安装dig/nslookup命令,使用下面命令安装: apt-get install dnsutils red-hat系列使用: yum install bind-utils ho ...

  2. Spark角色介绍及spark-shell的基本使用

    Spark角色介绍 1.Driver 它会运行客户端的main方法,构建了SparkContext对象,它是所有spark程序的入口 2.Application 它就是一个应用程序,包括了Driver ...

  3. windows下python安装Numpy和Scipy模块

    安装 numpy: 去 http://sourceforge.net/projects/numpy/files/latest/download?source=files 下载相应的exe安装文件. 安 ...

  4. DBArtist之Oracle入门第1步: 如何安装Oracle 11g

    操作系统:  Windows 7 数据库   :  Oracle 11gR2 第一步: 下载Oracle安装包 Oracle官网:  https://www.oracle.com/index.html ...

  5. 256. Paint House房屋染色

    [抄题]: There are a row of n houses, each house can be painted with one of the three colors: red, blue ...

  6. AspectJ AOP介绍

    idea下aspectj程序运行示例 有些同学可能想自己编写aspect程序进行测试练习,博主在这简单介绍运行环境的搭建,首先博主使用的idea的IDE,因此只对idea进行介绍.首先通过maven仓 ...

  7. jvisualvm远程监控服务器tomcat

    1.在 {服务器tomcat路径}/bin/catalina.sh 中,的[# OS specific support.  $var _must_ be set to either true or f ...

  8. 怎样下载网页中的js文件?

    1.按下F12,会看到调试的界面,如下图所示,先点击最上面的Resources选项,Frames--(你想下载的网址)--Script,下面就是全部的js文件啦,网站的图片啊什么的都可以下载了.

  9. LA4794 分享巧克力

    Sharing Chocolate Chocolate in its many forms is enjoyed by millions of people around the world ever ...

  10. linux c MQTT客户端实现

    linux c MQTT客户端实现 摘自:https://www.jianshu.com/p/d309de966379 一.前言:mqtt协议是轻量级的消息订阅和发布(publish/subscrib ...