官方API

Constructor Summary

ChromeDriver()

Creates a new ChromeDriver using the default server configuration.

ChromeDriver(ChromeDriverService service)

Creates a new ChromeDriver instance.The service will be started along with the driver, and shutdown upon calling RemoteWebDriver.quit().

ChromeDriver(ChromeOptions options)

Creates a new ChromeDriver instance with the specified options.

ChromeDriver(ChromeDriverService service, ChromeOptions options)

Creates a new ChromeDriver instance with the specified options. The service will be started along with the driver, and shutdown upon calling RemoteWebDriver.quit().

ChromeDriver(Capabilities capabilities)

Deprecated. Use ChromeDriver(ChromeOptions) instead.

ChromeDriver(ChromeDriverService service, Capabilities capabilities)

Deprecated. Use ChromeDriver(ChromeDriverService, ChromeOptions)

注:chrome浏览器实例化现今只适用前四种,后两种已作废不用。

实例代码

  • 按照默认配置启动chrome
public static void main(String[] args) {
String chromebin = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe";//chrome启动文件路径
String chromedriver = "E:/**/**/**/chromedriver.exe";//chromedriver文件路径 /* 设定 chrome启动文件的位置, 若未设定则取默认安装目录的 chrome */
System.setProperty("webdriver.chrome.bin", chromebin);
/* 设定 chrome webdirver 的位置 ,若未设定则从path变量读取*/
System.setProperty("webdriver.chrome.driver", chromedriver); WebDriver driver = new ChromeDriver();
driver.get("http://www.baidu.com");
        driver.close();//关闭浏览器
driver.quit();//退出浏览器
}
  • 使用服务管理chrome
public static void main(String[] args) {
String chromedriver = "E:/**/**/**/chromedriver.exe"; ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File(chromedriver))
.usingAnyFreePort().build();
WebDriver driver = new ChromeDriver(service);
driver.get("http://www.baidu.com");
driver.quit();
}
  • 自定义配置启动chrome
import java.io.File;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions; public class test {
public static void main(String[] args) {
String chromebin = "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe";
String chromedriver = "E:/**/**/**/chromedriver.exe"; /* 设定 chrome启动文件的位置, 若未设定则取默认安装目录的 chrome */
System.setProperty("webdriver.chrome.bin", chromebin);
/* 设定 chrome webdirver 的位置 ,若未设定则从path变量读取*/
System.setProperty("webdriver.chrome.driver", chromedriver); WebDriver driver = new ChromeDriver(setChromeOptions());
driver.get("http://www.baidu.com");
driver.close();//关闭浏览器
driver.quit();//退出浏览器
}
/**
* 设置 Chrome 浏览器的启动配置
* @return ChromeOptions Chrome 参数设置
*/
public static ChromeOptions setChromeOptions(){
ChromeOptions options = new ChromeOptions();
/*
* 设置参数
* --start-maximized 浏览器最大化
* test-type 忽略认证错误警示--ignore-certificate-errors
* */
options.addArguments("--start-maximized");
options.addArguments("test-type"); /*
* 加载插件
* files\\youtube.crx 代表查件文件路径
* */
File file = new File ("files\\youtube.crx");
options.addExtensions(file); return options;
}
}
  • 自定义配置,使用服务启动chrome
import java.io.File;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions; public class test { public static void main(String[] args) {
String chromedriver = "E:/**/**/**/chromedriver.exe"; ChromeDriverService service = new ChromeDriverService.Builder()
.usingDriverExecutable(new File(chromedriver))
.usingAnyFreePort().build();
WebDriver driver = new ChromeDriver(service,setChromeOptions());
driver.get("http://www.baidu.com");
driver.quit();
} /**
* 设置 Chrome 浏览器的启动配置
* @return ChromeOptions Chrome 参数设置
*/
public static ChromeOptions setChromeOptions(){
ChromeOptions options = new ChromeOptions();
/*
* 设置参数
* --start-maximized 浏览器最大化
* test-type 忽略认证错误警示--ignore-certificate-errors
* */
options.addArguments("--start-maximized");
options.addArguments("test-type"); /*
* 加载插件
* files\\youtube.crx 代表查件文件路径
* */
File file = new File ("files\\youtube.crx");
options.addExtensions(file); return options;
}
}

注意:为避免路径问题,chrome浏览器建议安装在默认路径下

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

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

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

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

    selenium操作chrome浏览器需要有ChromeDriver驱动来协助.一.什么是ChromeDriver?ChromeDriver是Chromium team开发维护的,它是实现WebDri ...

  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. ios的xxxAppDelegate.h分析

    #import "BIDAppDelegate.h" #import "BIDViewController.h" @implementation BIDAppD ...

  2. Simple Cubemap Reflection

    [Simple Cubemap Reflection] Cubemap加在MainTex上,所以Property需要按如下定义: 注意_Cubemap的类型是CUBE. 使用Cubemap,需要计算反 ...

  3. HQL多表查询

    ------------------siwuxie095 HQL 多表查询 以客户和联系人为例(一对多) 1.内连接 (1)hql 语句写法 from Customer c inner join c. ...

  4. Python实现常见算法[3]——汉罗塔递归

    #!/usr/bin/python # define three list var. z1 = [1,2,3,4,5,6,7,"1st zhu"] z2 = ["2st ...

  5. spring4-5-事务管理

    1.简单介绍 事务管理是企业级应用程序开发中必不可少的技术,  用来确保数据的完整性和一致性. 事务就是一系列的动作, 它们被当做一个单独的工作单元. 这些动作要么全部完成, 要么全部不起作用 事务的 ...

  6. 40 Questions to test your skill in Python for Data Science

    Comes from: https://www.analyticsvidhya.com/blog/2017/05/questions-python-for-data-science/ Python i ...

  7. JMeter下载及安装配置完整版

    特别需要注意的时,jdk版本和jmeter版本匹配问题. Jdk1.8对应apache-jmeter-3.3 Jmeter下载及安装配置 本文是在win7环境下安装使用jmeter,jmeter可以运 ...

  8. [c++] How many bytes do pointers take up?

    How many bytes do pointers take up? on 16 bit systems take up 2 bytes on 32 bit systems take up 4 by ...

  9. python3--json序列化

    # Auther: Aaron Fan # 把数据存入到一个文件中 # json格式的数据几乎可以通用语任何编程语言,但是仅仅只是简单的格式转换# 比如:字典.列表.元组.字符串这些,像函数.类就不可 ...

  10. ssh 连接缓慢解决方法

    ssh 连接缓慢解决方法 摘自:https://blog.csdn.net/qq_14821541/article/details/61915589 2017年03月13日 12:00:38 所以怎样 ...