zoukankan      html  css  js  c++  java
  • selenium(java)处理HTML5的视频播放

    大多数浏览器使用控件(如 Flash) 来播放规频,但是,不同的浏览器需要使用不同的插件。HTML5 定义了一个新的元素<video>,,指定了一个标准的方式来嵌入电影片段。IE9+、Firefox、Opera、Chrome都支持该元素。

    学习虫师的自动化测试selenium 的4.17节 《处理 HTML5 的视频播放》。但是无法运行该章节的demo,所以自己搞了一套。从HTML5写代码开始。

    首先要保证环境可用

    MP4视频在我本机装的火狐firefox浏览器不支持运行,索性使用chrome。之前写selenium脚本都是基于FirefoxDriver,所以需要下载ChromeDriver。需要注意,selenium版本+ChromeDriver版本+Chrome版本一定要匹配,不然会报错org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser。据说firefox的驱动还是selenium得人员开发的,而google的驱动是google内部人员开发的,兼容性欠佳。

    具体版本匹配可以参考:

    http://blog.csdn.net/xqhadoop/article/details/77892796

    我目前使用的是selenium 2.49+chrome driver 2.31+chrome version 59

    Chrome Driver下载完毕后,可以通过以下两种方法指定ChromeDriver位置:

    1)通过配置ChromeDriver.exe位置到path环境变量实现。

    2)通过webdriver.chrome.driver.系统属性实现。实现代码如下:

    System.setProperty("webdriver.chrome.driver", "E:\selenium\chromedriver_win32\chromedriver.exe");        
    WebDriver driver = new ChromeDriver();

    准备工作完成,写一个最简单的打开浏览器的脚本试一下:

    package video;
     
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
     
    public class Video3 {
        public static void main(String[] args) throws InterruptedException {
            
            System.out.println("chrome driver");
            
            // ChromeDriver没有放在C盘,所以使用 System 的 setProperty()方法指定浏览器驱动的路径
            System.setProperty("webdriver.chrome.driver", "E:\selenium\chromedriver_win32\chromedriver.exe");
            
            WebDriver driver = new ChromeDriver();
            
            // 打开浏览器
            driver.get("http://www.baidu.com");
            
            Thread.sleep(3000);
            
            driver.quit();
            
            
        }
    }

    在Chrome浏览器上顺利打开,说明驱动没有问题。
    接下来准备html文档,先随便下载一个MP4格式的文件,存放在本地,此处存放的路径为E:/selenium/aaa.mp4
    然后设置controls=“controls”,可控制播放暂停。存放这个html的路径为file:///C:/Users/Administrator/Desktop/video2.html 

    <!DOCTYPE html>
    <html>
    <head>
    <title>HTML5-video</title>
    </head>
    <body>
     
    <video width="320" height="240" controls="controls">
      <source src="E:/selenium/aaa.mp4" type="video/mp4">
      您的浏览器不支持 video 属性。
    </video>
     
    </body>
    </html>

    最后基于myEclipse编写selenium脚本

    Chrome浏览器可以使用自带的developer tools定位视频播放控件的xpath,具体方法可参考http://blog.csdn.net/mayanyun2013/article/details/72148734

    WebElement video = driver.findElement(By.xpath("/html/body/video"));

    完整代码:

    package video;
     
    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
     
    public class Video2 {
        public static void main(String[] args) throws InterruptedException {
            
            System.out.println("chrome driver");
            
            // ChromeDriver没有放在C盘,所以使用 System 的 setProperty()方法指定浏览器驱动的路径
            System.setProperty("webdriver.chrome.driver", "E:\selenium\chromedriver_win32\chromedriver.exe");
            
            WebDriver driver = new ChromeDriver();
            
            // 打开浏览器
            driver.get("file:///C:/Users/Administrator/Desktop/video2.html");
            
            // 定位视频播放控件
            WebElement video = driver.findElement(By.xpath("/html/body/video"));
            
            JavascriptExecutor jse = (JavascriptExecutor)driver;
            
            //获得视频的URL
            jse.executeScript("return arguments[0].currentSrc", video);
            
            // 播放视频,播放15秒
            jse.executeScript("return arguments[0].play()", video);
            Thread.sleep(15000);
            
            // 暂停视频
            jse.executeScript("arguments[0].pause()", video);
            
            driver.quit();
        }
     
    }

    JavaScript 函数有个内置的对象叫作 arguments。argument 对象包含了函数调用的参数数组,[0]表示取对象的第1个值。
    currentSrc 熟悉返回当前音频/视频的 URL。如果未设置音频/视频,则返回空字符串。
    load()、play()、pause() 等控制着视频的加载、播放和暂停。

    --------------------- 本文来自 ab_2016 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/ab_2016/article/details/78412520?utm_source=copy 

  • 相关阅读:
    Scala In 5 Years – My Prediction « GridGain – Real Time Big Data
    牛魔王珍满福拉面 北京团购网|京东团购
    Build WebKit On Windows 白果果白的专栏 博客频道 CSDN.NET
    fabulous
    Selenimu做爬虫 oscarxie 博客园
    EA gpl
    数据挖掘与Taco Bell编程
    test cpp could not compiled on ubuntu use g++,i'll tried lateor on win platform
    How to use ActiveRecord without Rails
    svn添加新项目管理并添加到trac 知识是一种越吃越饿的粮食 ITeye技术网站
  • 原文地址:https://www.cnblogs.com/xinxin1994/p/9755552.html
Copyright © 2011-2022 走看看