Java chrome driver的封裝


廢話不多說,直接上代碼

ChromeUtil

import org.openqa.selenium.Dimension;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* 封裝chrome driver
* @author songmin
*/
 
         
public class ChromeUtil {

private String downLoadPath = null;
private boolean isHeadless = false;
private boolean noPicture = false;
private boolean developmentMode = false;
private Integer height = 800;
private Integer width = 1300;

/**設置無頭模式*/
public void setHeadless(boolean isHeadless){
this.isHeadless = isHeadless;
}

/**設置無圖模式*/
public void setNoPicture(boolean noPicture){
this.noPicture = noPicture;
}

/**設置默認下載地址*/
public void setDownLoadPath(String path){
this.downLoadPath = path;
}

/**設置是否為開發者模式*/
public void setDevelopmentMode(boolean isDevelop){
this.developmentMode = isDevelop;
}

/**設置瀏覽器的大小*/
public void setHeightWithWidth(Integer height,Integer width){
this.height = height;
this.width = width;
}

/**獲得一個chrome對象*/
public ChromeDriver getChorme(){
//調用chrome driver
System.setProperty("webdriver.chrome.driver", "C:/Program Files (x86)/Google/Chrome/Application/chromedriver.exe");
DesiredCapabilities caps = initChromeOption();
//調用chrome
ChromeDriver driver = new ChromeDriver(caps);
//調整瀏覽器大小
driver.manage().window().setSize(new Dimension(width,height));
return driver;
}

/**初始化設置*/
public DesiredCapabilities initChromeOption() {
ChromeOptions options = new ChromeOptions();
DesiredCapabilities caps = new DesiredCapabilities();
if (isHeadless){
options.addArguments("-headless");
}
if (noPicture){
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("profile.managed_default_content_settings.images", 2);
options.setExperimentalOption("prefs", prefs);
}
if (downLoadPath!=null){
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("download.default_directory", downLoadPath);
chromePrefs.put("profile.default_content_settings.popups",0);
options.setExperimentalOption("prefs", chromePrefs);
}
if (developmentMode){
List excludeSwitches = new ArrayList<String>();
excludeSwitches.add("enable-automation");
options.setExperimentalOption("excludeSwitches", excludeSwitches);
}
     
caps.setCapability(ChromeOptions.CAPABILITY, options);
        return caps;
}
}
 

使用示例

 
         
/**
* 封裝chrome driver
* @author songmin
*/

public
class Test { public static void main(String[] args) throws InterruptedException { ChromeUtil chromeUtil = new ChromeUtil(); //設置為開發者模式 chromeUtil.setDevelopmentMode(true); //設置瀏覽器的高寬 chromeUtil.setHeightWithWidth(400,400); //設置為無頭模式 chromeUtil.setHeadless(true); ChromeDriver driver = chromeUtil.getChorme(); driver.get("http://www.ushowtime.cn"); Thread.sleep(3000); driver.close(); } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM