官方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. 【转】iphone - ios app maximum memory budget

    https://stackoverflow.com/questions/5887248/ios-app-maximum-memory-budget device: (crash amount/tota ...

  2. dev GridControl显示标题

    gridview:ShowViewCaption = TrueViewCation = "32435354354"

  3. 并发编程和MySQL总结

    1️⃣  并发编程主要内容: 操作系统工作原理介绍.线程.进程演化史.特点.区别.互斥锁.信号.事件.join.GIL.进程间通信.管道.队列. 生产者消费者模型.异步模型.IO多路复用模型.sele ...

  4. NormalMapping

    [NormalMapping] 法线贴图内的数据是法线,高度贴图内的数据是高度,不是一个东西.在ShaderLab中,UnpackNormal()分析的是法线贴图(注意不是高度贴图). 可以看到,在G ...

  5. GameObject.Active

    [GameObject.Active] 用于控制一个对象是否激活,一个对象激活当且本身active=true,并且它的父结点也都active.相当API有: 1)GameObject.SetActiv ...

  6. 使用vue-cli创建一个vue项目

    安装vue-cli npm install -g @vue/cli 1, 使用vue创建一个项目 vue create luffy 2, 安装所需的插件 npm install vue-router ...

  7. Spark分布式计算执行模型

    引言 相对Hadoop, Spark在处理需要迭代运算的机器学习训练等任务上有着很大性能提升,同时提供了批处理.实时数据处理.机器学习以及图算法等一站式的服务,因此最近大家一起来学习Spark,特别是 ...

  8. How to Get the Length of File in C

    How to get length of file in C //=== int fileLen(FILE *fp) { int nRet = -1; int nPosBak; nPosBak = f ...

  9. LA4794 分享巧克力

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

  10. linux的“自动化”-乾颐堂CCIE

    linux系统的web网站在运营状态时,我们常需要对网站进行维护,例如查看资源剩余并做出响应.日志分割.数据整理,在特定状态执行特定任务等等,这些都会需要linux能实现自动执行某些任任务.本篇博文介 ...