官方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瀏覽器建議安裝在默認路徑下
?
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061
微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元

