zoukankan      html  css  js  c++  java
  • linux+java+webdriver chrome handless无界面启动

    网上现有的解决方案要么是windows下的,要么是python的,搞了一天终于解决了,记录如下。

    1 下载chrome linux版和对应版本的webdriver,我这里使用的是chrome66和chromedriver2.38,selenium版本选最新的就行,我这里用的是3.11

      chrome:自己找

      selenium:自己找

      chromedriver:http://npm.taobao.org/mirrors/chromedriver/

    2 java代码 

      System.setProperty("webdriver.chrome.driver", "/usr/bin/chromedriver");  //chromederiver存放位置
      System.setProperty("webdriver.chrome.bin", "/opt/google/chrome/chrome");  //chrome安装位置
      ChromeOptions options = new ChromeOptions();

      options.addArguments("headless");  //无界面参数
      options.addArguments("no-sandbox");  //禁用沙盒 就是被这个参数搞了一天

      WebDriver driver = new ChromeDriver(options);

      driver.get("http://www.baidu.com");

      System.out.println(driver.getTitle());

    3 解决思路

      按网上的方法整了半天都没搞定,最后一直查资料,查到作者写的说明,说可以在linux下使用如下参数无界面启动chrome

        ./chrome --handless

      使用此命令后报错ERROR:zygote_host_impl_linux.cc(88)] Running as root without --no-sandbox is not supported.

      意思是在root用户下不能开启沙箱模式!

      于是用如下参数就正常启动了!

        ./chrome --handless --no-sandbox

      所以在java代码里写入该参数就行了!

    参考:

    http://www.cnblogs.com/technologylife/p/5829944.html

    https://developers.google.cn/web/updates/2017/04/headless-chrome

    https://intoli.com/blog/running-selenium-with-headless-chrome/

  • 相关阅读:
    get和post的区别
    关于webWorker的理解和简单例子
    JavaScript停止事件冒泡和取消事件默认行为
    深入理解js构造函数
    js之yeild
    文件组织方式
    HTML5新增的标签和属性归纳
    css3新增属性
    CSS3 transition介绍
    Mysql安装
  • 原文地址:https://www.cnblogs.com/cation/p/8954918.html
Copyright © 2011-2022 走看看