selenium中啓動chrome瀏覽器時加載插件

      使用selenium啓動的chrome瀏覽器,一般是乾淨的瀏覽器,如果需要使用某個插件,那麼啓動瀏覽器時,就需要加載插件,

代碼如下:

import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

/*
 * chrome瀏覽器啓動時加載某個插件
 */

public class KeywordBrowserChromePlug {

	public static void main(String[] args) {
		//存放插件的位置
		File plugPath = new File("D:\\google_fwzs\\fwzs.crx");
		
		//創建options對象
		ChromeOptions options = new ChromeOptions();
		
		//添加插件到chrome瀏覽器中
		options.addExtensions(plugPath);
		
		System.setProperty("webdriver.chrome.driver", "D:\\chromedriver\\chromedriver.exe");
		
		//實例化driver時加載ChromeOptions
		WebDriver driver = new ChromeDriver(options);
		
		driver.get("http://www.baidu.com");
	}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章