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/

  • 相关阅读:
    git add 添加错文件 撤销
    工作流Activiti5.13学习笔记(一)
    instanceof, isinstance,isAssignableFrom的区别
    oracle表查询速度极慢的处理过程记录一下
    类里面的成员变量如果是public,为什么破坏封装
    IPv4 IPv6验证
    枚举使用
    XML之命名空间的作用(xmlns)
    XSD-JAVA
    jaxb
  • 原文地址:https://www.cnblogs.com/cation/p/8954918.html
Copyright © 2011-2022 走看看