3
import org.openqa.selenium.WebDriver;

public interface WebDriverPool {
    WebDriver get() throws InterruptedException;
    void returnToPool(WebDriver webDriver);
    void close(WebDriver webDriver);
    void shutdown();
}
import java.util.ArrayList;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import net.xby1993.common.util.FileUtil;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * @author taojw
 */
public class PhantomjsWebDriverPool implements WebDriverPool{
    private Logger logger = LoggerFactory.getLogger(getClass());

    private int CAPACITY = 5;
    private AtomicInteger refCount = new AtomicInteger(0);
    private static final String DRIVER_PHANTOMJS = "phantomjs";

    /**
     * store webDrivers available
     */
    private BlockingDeque<WebDriver> innerQueue = new LinkedBlockingDeque<WebDriver>(
            CAPACITY);

    private  String PHANTOMJS_PATH;
    private  DesiredCapabilities caps = DesiredCapabilities.phantomjs();

    public PhantomjsWebDriverPool() {
        this(5,false);
    }

    /**
     * 
     * @param poolsize 
     * @param loadImg 是否加载图片,默认不加载
     */
    public PhantomjsWebDriverPool(int poolsize,boolean loadImg) {
        this.CAPACITY = poolsize;
        innerQueue = new LinkedBlockingDeque<WebDriver>(poolsize);
        PHANTOMJS_PATH = FileUtil.getCommonProp("phantomjs.path");
        caps.setJavascriptEnabled(true);
        caps.setCapability(
                PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                PHANTOMJS_PATH);
//        caps.setCapability("takesScreenshot", false);
        caps.setCapability(
                PhantomJSDriverService.PHANTOMJS_PAGE_CUSTOMHEADERS_PREFIX
                        + "User-Agent",
                "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36");
        ArrayList<String> cliArgsCap = new ArrayList<String>();
        //http://phantomjs.org/api/command-line.html
        cliArgsCap.add("--web-security=false");
        cliArgsCap.add("--ssl-protocol=any");
        cliArgsCap.add("--ignore-ssl-errors=true");
        if(loadImg){
            cliArgsCap.add("--load-images=true");
        }else{
            cliArgsCap.add("--load-images=false");
        }
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS,
                cliArgsCap);
        caps.setCapability(
                PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS,
                new String[] {"--logLevel=INFO"});
        
    }

    public WebDriver get() throws InterruptedException {
        WebDriver poll = innerQueue.poll();
        if (poll != null) {
            return poll;
        }
        if (refCount.get() < CAPACITY) {
            synchronized (innerQueue) {
                if (refCount.get() < CAPACITY) {

                    WebDriver mDriver = new PhantomJSDriver(caps);
                    // 尝试性解决:https://github.com/ariya/phantomjs/issues/11526问题
                    mDriver.manage().timeouts()
                            .pageLoadTimeout(60, TimeUnit.SECONDS);
                    // mDriver.manage().window().setSize(new Dimension(1366,
                    // 768));
                    innerQueue.add(mDriver);
                    refCount.incrementAndGet();
                }
            }
        }
        return innerQueue.take();
    }

    public void returnToPool(WebDriver webDriver) {
        // webDriver.quit();
        // webDriver=null;
        innerQueue.add(webDriver);
    }

    public void close(WebDriver webDriver) {
        refCount.decrementAndGet();
        webDriver.quit();
        webDriver = null;
    }

    public void shutdown() {
        try {
            for (WebDriver driver : innerQueue) {
                close(driver);
            }
            innerQueue.clear();
        } catch (Exception e) {
//            e.printStackTrace();
            logger.warn("webdriverpool关闭失败",e);
        }
    }
}
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import net.xby1993.common.util.FileUtil;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ChromeWebDriverPool implements WebDriverPool{
    private Logger logger = LoggerFactory.getLogger(getClass());

    private int CAPACITY = 5;
    private AtomicInteger refCount = new AtomicInteger(0);
    private static final String DRIVER_NAME = "chrome";

    /**
     * store webDrivers available
     */
    private BlockingDeque<WebDriver> innerQueue = new LinkedBlockingDeque<WebDriver>(
            CAPACITY);

    private static String DRIVER_PATH;
    private static DesiredCapabilities caps = DesiredCapabilities.chrome();
    static {
        DRIVER_PATH = FileUtil.getCommonProp("chrome.path");
        
        System.setProperty("webdriver.chrome.driver", FileUtil.getCommonProp("chrome.driver.path"));

        ChromeOptions options = new ChromeOptions();
//        options.addExtensions(new File("/path/to/extension.crx"))
//        options.setBinary(DRIVER_PATH); //注意chrome和chromeDirver的区别
        options.addArguments("test-type"); //ignore certificate errors
        options.addArguments("headless");// headless mode
        options.addArguments("disable-gpu"); 
//        options.addArguments("log-path=chromedriver.log");
//        options.addArguments("screenshot"); 没打开一个页面就截图
        //options.addArguments("start-maximized"); 最大化
        //Use custom profile
        Map<String, Object> prefs = new HashMap<String, Object>();
//        prefs.put("profile.default_content_settings.popups", 0);
        //http://stackoverflow.com/questions/28070315/python-disable-images-in-selenium-google-chromedriver
        prefs.put("profile.managed_default_content_settings.images",2); //禁止下载加载图片
        options.setExperimentalOption("prefs", prefs);
        
        
        caps.setJavascriptEnabled(true);
        caps.setCapability(ChromeOptions.CAPABILITY, options);        
//        caps.setCapability("takesScreenshot", false);
        
        /* Add the WebDriver proxy capability.
        Proxy proxy = new Proxy();
        proxy.setHttpProxy("myhttpproxy:3337");
        capabilities.setCapability("proxy", proxy);
        */

    }

    public ChromeWebDriverPool() {
    }

    public ChromeWebDriverPool(int poolsize) {
        this.CAPACITY = poolsize;
        innerQueue = new LinkedBlockingDeque<WebDriver>(poolsize);
    }

    public WebDriver get() throws InterruptedException {
        WebDriver poll = innerQueue.poll();
        if (poll != null) {
            return poll;
        }
        if (refCount.get() < CAPACITY) {
            synchronized (innerQueue) {
                if (refCount.get() < CAPACITY) {

                    WebDriver mDriver = new ChromeDriver(caps);
                    // 尝试性解决:https://github.com/ariya/phantomjs/issues/11526问题
                    mDriver.manage().timeouts()
                            .pageLoadTimeout(60, TimeUnit.SECONDS);
                    // mDriver.manage().window().setSize(new Dimension(1366,
                    // 768));
                    innerQueue.add(mDriver);
                    refCount.incrementAndGet();
                }
            }
        }
        return innerQueue.take();
    }

    public void returnToPool(WebDriver webDriver) {
        // webDriver.quit();
        // webDriver=null;
        innerQueue.add(webDriver);
    }

    public void close(WebDriver webDriver) {
        refCount.decrementAndGet();
        webDriver.quit();
        webDriver = null;
    }

    public void shutdown() {
        try {
            for (WebDriver driver : innerQueue) {
                close(driver);
            }
            innerQueue.clear();
        } catch (Exception e) {
            // e.printStackTrace();
            logger.warn("webdriverpool关闭失败", e);
        }
    }
}
import java.io.Closeable;
import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class WebDriverManager implements Closeable{
    private static final Logger log=LoggerFactory.getLogger(WebDriverManager.class);
    
    private WebDriverPool webDriverPool=null;
    
    public WebDriverManager(){
        this.webDriverPool=new PhantomjsWebDriverPool(1,false);
    }
    public WebDriverManager(WebDriverPool webDriverPool){
        this.webDriverPool=webDriverPool;
    }
    public void load(String url,int sleepTimeMillis,SeleniumAction... actions){
        WebDriver driver=null;
        try {
            driver=webDriverPool.get();
            driver.get(url);
            sleep(sleepTimeMillis);
            WebDriver.Options manage = driver.manage();
            manage.window().maximize();
            for(SeleniumAction action:actions){
                action.execute(driver);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.error("",e);
        }finally{
            if(driver!=null){
                webDriverPool.returnToPool(driver);
            }
        }
    }
    public void load(SeleniumAction... actions){
        WebDriver driver=null;
        try {
            driver=webDriverPool.get();
            WebDriver.Options manage = driver.manage();
            manage.window().maximize();
            for(SeleniumAction action:actions){
                action.execute(driver);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
            log.error("",e);
        }finally{
            if(driver!=null){
                webDriverPool.returnToPool(driver);
            }
        }
    }
    public void shutDown(){
        if(webDriverPool!=null){
            webDriverPool.shutdown();
        }
    }
    @Override
    public void close() throws IOException {
        shutDown();
    }
    public void sleep(long millis){
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    
}
import org.openqa.selenium.WebDriver;

/**
 * @author taojw
 *
 */
public interface SeleniumAction {
    void execute(WebDriver driver);
}
import java.io.File;
import java.io.IOException;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

/**
 * @author taojw
 *
 */
public class WindowUtil {
    
    /**
     * 滚动窗口。
     * @param driver
     * @param height
     */
    public static void scroll(WebDriver driver,int height){
        ((JavascriptExecutor)driver).executeScript("window.scrollTo(0,"+height+" );");    
    }
    /**
     * 重新调整窗口大小,以适应页面,需要耗费一定时间。建议等待合理的时间。
     * @param driver
     */
    public static void loadAll(WebDriver driver){
        Dimension od=driver.manage().window().getSize();
        int width=driver.manage().window().getSize().width;
        //尝试性解决:https://github.com/ariya/phantomjs/issues/11526问题
        driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS); 
        long height=(Long)((JavascriptExecutor)driver).executeScript("return document.body.scrollHeight;");
        driver.manage().window().setSize(new Dimension(width, (int)height));
        driver.navigate().refresh();
    }
    public static void refresh(WebDriver driver){
        driver.navigate().refresh();
    }
    public static void taskScreenShot(WebDriver driver,File saveFile){
        if(saveFile.exists()){
            saveFile.delete();
        }
        File src=((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(src, saveFile);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static void changeWindow(WebDriver driver){
        // 获取当前页面句柄
        String handle = driver.getWindowHandle();
        // 获取所有页面的句柄,并循环判断不是当前的句柄,就做选取switchTo()
        for (String handles : driver.getWindowHandles()) {
            if (handles.equals(handle))
                continue;
            driver.switchTo().window(handles);
        }
    }
    public static void changeWindowTo(WebDriver driver,String handle){
        for (String tmp : driver.getWindowHandles()) {
            if (tmp.equals(handle)){
                driver.switchTo().window(handle);
                break;
            }
        }
    }
    
    /**
     * 打开一个新tab页,返回该tab页的windowhandle
     * @param driver
     * @param url
     * @return
     */
    public static String openNewTab(WebDriver driver,String url){
        Set<String> strSet1=driver.getWindowHandles();
        ((JavascriptExecutor)driver).executeScript("window.open('"+url+"','_blank');");
        sleep(1000);
        Set<String> strSet2=driver.getWindowHandles();
        for(String tmp:strSet2){
            if(!strSet1.contains(tmp)){
                return tmp;
            }
        }
        return null;
    }
    public static void sleep(long millis){
        try {
            Thread.sleep(millis);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    /**
     * 操作关闭模态窗口
     * @param driver
     * @param type 如Id,ClassName
     * @param sel 选择器
     */
    public static void clickModal(WebDriver driver,String type,String sel){
        String js="document.getElementsBy"+type+"('"+sel+"')[0].click();";
        ((JavascriptExecutor)driver).executeScript(js);
    }
    
    /**
     * 判断一个元素是否存在
     * @param driver
     * @param by
     * @return
     */
    public static boolean checkElementExists(WebDriver driver,By by){
        try{
            driver.findElement(by);
            return true;
        }catch(NoSuchElementException e){
            return false;
        }
    }
    /**
     * 点击一个元素
     * @param driver
     * @param by
     */
    public static void clickElement(WebDriver driver,By by){
        WebElement tmp=driver.findElement(by);
        Actions actions=new Actions(driver);
        actions.moveToElement(tmp).click().perform();
    }
    public static void clickElement(WebDriver driver,WebElement tmp){
        Actions actions=new Actions(driver);
        actions.moveToElement(tmp).click().perform();
    }
    public static Object execJs(WebDriver driver,String js){
        return ((JavascriptExecutor)driver).executeScript(js);
    }
    public static void clickByJsCssSelector(WebDriver driver,String cssSelector){
        String js="document.querySelector('"+cssSelector+"').click();";
        ((JavascriptExecutor)driver).executeScript(js);
    }
    
    public static Set<Cookie> getCookies(WebDriver driver){
        return driver.manage().getCookies();
    }
    public static void setCookies(WebDriver driver,Set<Cookie> cookies){
        if(cookies==null){
            return;
        }
        //Phantomjs存在Cookie设置bug,只能通过js来设置了。
        StringBuilder sb=new StringBuilder();
        for(Cookie cookie:cookies){
            String js="document.cookie=\""+cookie.getName()+"="+cookie.getValue()+";path="+cookie.getPath()+";domain="+cookie.getDomain()+"\";";
            sb.append(js);
        }
        ((JavascriptExecutor)driver).executeScript(sb.toString());
    }
    
    public static String getHttpCookieString(Set<Cookie> cookies){
        String httpCookie="";
        int index=0;
        for(Cookie c:cookies){
            index++;
            if(index==cookies.size()){
                httpCookie+=c.getName()+"="+c.getValue();
            }else{
                httpCookie+=c.getName()+"="+c.getValue()+"; ";
            }
        }
        return httpCookie;
    }
}

xbynet
1k 声望124 粉丝

不雨花犹落,无风絮自飞