【Selenium专题】WebDriver启动Chrome浏览器(二)
官方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 |
|
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浏览器(二)的更多相关文章
- selenium webdriver启动Chrome浏览器后无法输入网址的解决办法
通过selenium webdriver启动Chrome浏览器,脚本如下: from selenium import webdriver browser = webdriver.Chrome() br ...
- 【Selenium专题】WebDriver启动Chrome浏览器(一)
selenium操作chrome浏览器需要有ChromeDriver驱动来协助.一.什么是ChromeDriver?ChromeDriver是Chromium team开发维护的,它是实现WebDri ...
- 设置Webdriver启动chrome为默认用户的配置信息
Webdriver 启动Chrome浏览器时,默认是打开一个新用户,而非默认用户.即新用户没有我们安装扩展程序.但在实际应用中,我们会须要 默认用户安装的一些扩展程序,比方对于某些js或者css样式. ...
- selenium webdriver 启动三大浏览器Firefox,Chrome,IE
selenium webdriver 启动三大浏览器Firefox,Chrome,IE 1.安装selenium 在联网的情况下,在Windows命令行(cmd)输入pip install selen ...
- Selenium启动Chrome浏览器提示“请停用以开发者模式运行的扩展程序”的解决办法
安装了python selenium,运行下面代码: 1 from selenium import webdriver 2 3 browser = webdriver.Chrome() 4 brows ...
- selenium webdriver启动IE浏览器失败的解决办法
通过selenium webdriver启动IE浏览器失败,报错:selenium.common.exceptions.WebDriverException: Message: Unexpected ...
- Java&Selenium根据实参启动相应浏览器
Java&Selenium根据实参启动相应浏览器 /** * 定义函数initBrowser * @param browser:字符串参数chrome/ie/xx * @return 并返回驱 ...
- 启动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", ...
- ChromeDriver启动Chrome浏览器后,地址栏只显示data;——chromeDriver版本不对
ChromeDriver启动Chrome浏览器后,地址栏只显示data; 错误原因: chromeDriver版本不对,不同版本的chromeDriver对应不同版本的chrome浏览器 chrome ...
随机推荐
- Spring 学习记录4 ResourceLoader
ResourceLoader Spring的ApplicationContext继承了ResourceLoader接口.这个接口主要就是可以加载各种resource.. 接口还是比较简单的: /* * ...
- MySQL用变量的方法添加伪序号列(自增序列)
在进行数据筛选时,可能会用到给每一条数据配上一个唯一的序号,便于进行定位. 方法: 序号的设置: @rownum :=@rownum + 1 AS rownum 获取序号的伪表[必须]: (S ...
- 通过devtools在centos系统中启用高版本的gcc
C++11出来好久了,现在还是使用c++03的,需要在centos6.6的系统上实现gcc的升级,又不想自己编译代码. 于是选用了devtoolsset系列,安装脚本如下 安装脚本如下 functio ...
- oracel 查询删除重复记录的几种方法
建表语句CREATE TABLE Persons(PersonID int, LastName varchar(255),FirstName varchar(255),Addres ...
- VUE+WebPack游戏开发:神庙逃亡的游戏设计
- 124. Binary Tree Maximum Path Sum (Tree; DFS)
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence ...
- 多个div嵌套,获取鼠标所点击的div对象
我选择的是冒泡事件 $(function() { $("#主divID").on("click",function(e) {//主div是必须存在的 //冒泡事 ...
- Nginx概述、安装及配置详解
nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...
- appium_server_v1.4.16版本不适配android7.0系统,运行报错“Attempt to re-install io.appium.settings without first uninstalling”
要解决的问题:appium在androidV7.0系统上运行时报错 Failure [INSTALL_FAILED_ALREADY_EXISTS: Attempt to re-install io.a ...
- Cocos2dx之touch事件
今天看了下ccocos2dx touch事件部分的源码,从CCTouch.CCTouchHandler和CCTouchDispatcher简单的做了分析和总结,先直接看源码吧! 1.CCTouch c ...