linux下Selenium+Chrome+Chromedriver+java环境搭建+java代码示例

1、linux下chrome安装

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm

默认安装后执行文件路径是:

# 谷歌浏览器路径
path.chrome: /usr/bin/google-chrome

2、linux下Chromedriver安装

这个只需要下载对应系统版本就可以了,注意:chrome版本和chromedriver版本对应关系(参考:下载地址中有notes.txt做介绍)

notes.txt 
Chromedriver下载地址:http://npm.taobao.org/mirrors/chromedriver/

下载后复制到需要需要的目录就可以了,例如:

#chromedriver路径
chrome-driver: /usr/bin/chromedriver

注意要给chromedriver可执行权限:chmod 777 chromedriver

3、java工程中使用创建Webdriver示例

/**
 * 取得一个空闲的WebDriver.
 *
 * @param queueName 队列名称
 * @return org.openqa.selenium.WebDriver
 */
public static WebDriver getOneFreeDriver(String queueName, boolean useProxy) throws IOException {
    // 从队列里取
    WebDriver driver = driverQueue.poll(queueName);

    // 一、取到,直接返回
    if (null != driver) {
        return driver;
    }

    //二、未取到,创建
    ChromeOptions chromeOptions = new ChromeOptions();
    //针对linux环境设置
    if (OSinfo.isLinux()) {
        chromeOptions.addArguments("headless");//无界面参数
        chromeOptions.addArguments("no-sandbox");//禁用沙盒 就是被这个参数搞了一天
    }
    //使用代理
    if (useProxy) {
        //使用代理插件动态切换代理
        chromeOptions.addExtensions(new File(Const.PROXY_EXTENSION_PATH));
    }
    DesiredCapabilities chromeCap = DesiredCapabilities.chrome().merge(chromeOptions);

    //创建ChromeDriverService
    ChromeDriverService service = createChromeDriverService();
    driver = new RemoteWebDriver(service.getUrl(), chromeCap);
    //最大化窗口
    driver.manage().window().maximize();
    return driver;
}

 

linux下注意这几行:

    //针对linux环境设置
    if (OSinfo.isLinux()) {
        chromeOptions.addArguments("headless");//无界面参数
        chromeOptions.addArguments("no-sandbox");//禁用沙盒 就是被这个参数搞了一天
    }

猜你喜欢

转载自blog.csdn.net/hanxue6898/article/details/81184907
今日推荐