5.2 使用浏览器无头模式执行Selnium脚本

作者: 博客已迁移I米阳 | 来源:发表于2018-05-08 09:09 被阅读50次

执行Selenium脚本时,我们往往不需要盯着电脑看脚本的执行情况,再或者我们的脚本可能被执行在一个无GUI的Linux机器上,那么这时我们都可以使用浏览器的Headless模式来执行。

Headless Chrome

chrome版本要求:

  • windows 60+
  • mac/linux 59+

chromedriver版本要求:

  • 2.30+
@Test
    public void OpenChromeTest() {
        String path = System.getProperty("user.dir");
        System.setProperty("webdriver.chrome.driver", path + "\\drivers\\chromedriver.exe");
        ChromeOptions chromeOptions = new ChromeOptions();
//        设置为 headless 模式 (必须)
        chromeOptions.addArguments("--headless");
//        设置浏览器窗口打开大小  (非必须)
        chromeOptions.addArguments("--window-size=1920,1080");
        WebDriver driver = new ChromeDriver(chromeOptions);
        driver.get("http://www.baidu.com");
        String title = driver.getTitle();
        System.out.println(title);
        driver.quit();
    }

PhantomJS

PhantomJS是一款使用JavaScript API编写的Headless WebKit。它支持各种Web标准:DOM处理,CSS选择器,JSON,Canvas和SVG

PhantomJS环境准备

下载地址:http://phantomjs.org/download.html
解压获取bin目录下的PhantomJS.exe文件,并复制到工程路径下。

注意:如果Selenium版本<3.0,则需要配置Pom.xml 文件,添加如下

<!-- https://mvnrepository.com/artifact/org.webjars.npm/phantomjs -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>phantomjs</artifactId>
    <version>2.1.7</version>
</dependency>

代码例子:

    @Test
    public void pjsTest() throws InterruptedException {
        System.setProperty("phantomjs.binary.path", "./drivers/phantomjs.exe");
        WebDriver driver = new PhantomJSDriver();
        driver.get("http://www.baidu.com");
        String title = driver.getTitle();
        System.out.println(title);
        driver.quit();
    }

headless Firefox

Firefox版本要求:

  • windows/mac 56+
  • linux 55+

geckodriver

  • 建议都用最新版
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;

import java.util.concurrent.TimeUnit;

public class HeadlessFirefoxSeleniumExample {
 public static void main(String [] args) {
   FirefoxBinary firefoxBinary = new FirefoxBinary();
   firefoxBinary.addCommandLineOptions("--headless");
   System.setProperty("webdriver.gecko.driver", "/opt/geckodriver");
   FirefoxOptions firefoxOptions = new FirefoxOptions();
   firefoxOptions.setBinary(firefoxBinary);
   FirefoxDriver driver = new FirefoxDriver(firefoxOptions);
   try {
     driver.get("http://www.google.com");
     driver.manage().timeouts().implicitlyWait(4,
         TimeUnit.SECONDS);
     WebElement queryBox = driver.findElement(By.name("q"));
     queryBox.sendKeys("headless firefox");
     WebElement searchBtn = driver.findElement(By.name("btnK"));
     searchBtn.click();
     WebElement iresDiv = driver.findElement(By.id("ires"));
     iresDiv.findElements(By.tagName("a")).get(0).click();
     System.out.println(driver.getPageSource());
   } finally {
     driver.quit();
   }
 }
}

Headless Chrome 和 Headless Firefox 的出现是为了取代PhantomJS。 为啥呢?因为PhantomJS并非真在的浏览器,跟真实浏览器还存在一定的差异。当然除了上面介绍的三种无头模式外,Selenium 还自带了一个HttpUnit的无头模式(WebDriver driver = new HtmlUnitDriver())同样并非真在浏览器,而且对JS支持不好,一般很少用到。

详细见个人博客:http://www.jianshu.com/p/b01de206a0d7

欢迎关注个人公众号:


个人公众号

相关文章

  • 5.2 使用浏览器无头模式执行Selnium脚本

    执行Selenium脚本时,我们往往不需要盯着电脑看脚本的执行情况,再或者我们的脚本可能被执行在一个无GUI的Li...

  • 异步- async 和 defer

    无 async 和 defer浏览器立即加载并执行指定脚本(读到即加载并执行),阻塞文档解析 async脚本的加载...

  • java-selenium 实现网页截图

    使用firefox浏览器无头模式在内存里渲染页面,然后用selenium操作浏览器并解析截图。需要安装firefo...

  • mac上无头(headless)chrome开启失败

    Python selenium 调用chrome浏览器的无头模式这行代码已经执行 但是依旧会打开gui在网上找了很...

  • 大前端——知识点回顾(HTML)

    HTML 一、浏览器的标准模式和怪异模式? 标准模式:浏览器按W3C标准解析执行代码;怪异模式:使用浏览器自己的方...

  • 界面自动化脚本开发案例

    Selenium介绍 1)框架底层使用JavaScript模拟真实用户对浏览器进行操作。测试脚本执行时,浏览器自动...

  • 通过id方式定位元素失败解决

    Selenium介绍 1)框架底层使用JavaScript模拟真实用户对浏览器进行操作。测试脚本执行时,浏览器自动...

  • 外边距

    外补丁即为外边距 标准模式是指,浏览器按W3C标准解析执行代码;怪异模式则是使用浏览器自己的方式解析执行代码,因为...

  • Shell学习笔记-expect 自动交互脚本

    启用选项 -c:执行脚本前先执行的命令,可多次使用。 -d:debug模式,可以在运行时输出一些诊断信息,与在脚本...

  • 小程序:脚本的执行顺序问题

    脚本的执行顺序 浏览器中,脚本严格按照加载的顺序执行,如代码2-29所示。 代码清单2-29 浏览器中的脚本 以上...

网友评论

    本文标题:5.2 使用浏览器无头模式执行Selnium脚本

    本文链接:https://www.haomeiwen.com/subject/ubfisxtx.html