代码改变世界

selenium webdriver处理HTML5 的视频播放

2017-02-10 00:09  清风软件测试开发  阅读(2976)  评论(0编辑  收藏  举报
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;
public class Video {
      public static void main(String args[]) throws InterruptedException {
            WebDriver driver = new ChromeDriver();
            driver.get("http://videojs.com/");
            WebElement video = driver.findElement(By.id("home_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() 等等控制着视频的加载,播放和暂停。