selenium操作H5视频

测试网址:http://www.w3school.com.cn/tiy/loadtext.asp?f=html5_video_simple

package com.allin.pc;

import java.io.File;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.sun.jna.platform.FileUtils;

public class html5VedioPlay {
    public WebDriver driver;
    String baseUrl = "http://www.w3school.com.cn/tiy/loadtext.asp?f=html5_video_simple";
    
    @Test
    public void testVideoPlay() throws InterruptedException{
        //定义页面截图文件对象,用于后面的屏幕截图储存
        File captureScreenFile = null;
        //访问HTML5实现播放器的网页页面
        driver.get(baseUrl);
        //打印出HTML5视频播放器的源码
        System.out.println(driver.getPageSource());
        //获取页面中的video标签
        WebElement videoPlayer = driver.findElement(By.tagName("video"));
        //声明一个JavascriptExecutor对象
        JavascriptExecutor javascritExecutor = (JavascriptExecutor) driver;
        //使用JavaScriptExecutor对象执行JavaScript语句,通过播放器内部的
        //currentsrc属性获取视频文件的网络存储地址
        String videoSrc = (String) javascritExecutor.executeScript("return arguments[0].currentSrc;",
                videoPlayer);
        //输出视频文件的网络存储地址
        System.out.println(videoSrc);
        //断言视频网络地址是否合期望
        Assert.assertEquals("http://www.w3school.com.cn/i/movie.ogg", videoSrc);
        //使用JavascriptExecutor对象执行JavaScript语句,通过播放器内部的duration属性获取视频文件的播放时长
        Double videoDuration = (Double) javascritExecutor.executeScript("return arguments[0].duration;",
                videoPlayer);
        //输出视频的播放时长
        System.out.println(videoDuration.intValue());
        //等待5秒视频加载时间
        Thread.sleep(5000);
        //使用JavaScriptExecutor对象执行JavaScript语句,通过调用播放器内部的play函数来播放影片
        javascritExecutor.executeScript("return arguments[0].play();", videoPlayer);        
        Thread.sleep(2000);
        //播放2秒后,使用JavaScriptExecutor对象执行JavaScript语句,通过调用播放器内部的pause函数暂停播放影片
        javascritExecutor.executeScript("return arguments[0].pause();", videoPlayer);
        Thread.sleep(3000);
        //将暂停视频播放后的页面进行截图,并保存D盘上的videoPlay_pause.jpg文件
        captureScreenFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
        //FileUtils.copyFile(captureScreenFile, new File("d:\\videoPlay_pause.jpg"));
    }        
        @BeforeMethod
        public void setUp(){
            System.setProperty("webdriver.chrome.driver", "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chromedriver.exe");
            driver = new ChromeDriver();
            
        }
        
        @AfterMethod
        public void tearDown(){
            driver.quit();
        
    }
}

时间: 2024-08-16 16:04:53

selenium操作H5视频的相关文章

H5 视频直播相关技术

一.移动视频直播发展 大家首先来看下面这张图: 可以看到,直播从 PC 到一直发展到移动端,越来越多的直播类 App 上线,同时移动直播进入了前所未有的爆发阶段,但是对于大多数移动直播来说,还是要以 Native 客户端实现为主,但是 H5 在移动直播端也承载着不可替代的作用,例如 H5 有着传播快,易发布的优势,同时最为关键的时 H5 同样可以播放直播视频. 大家可以看下面这张大概的实现图 完整的直播可以分为以下几块: 视频录制端:一般是电脑上的音视频输入设备或者手机端的摄像头或者麦克风,目前

H5视频直播扫盲

H5视频直播扫盲 2016-05-25 • 前端杂项 • 14 条评论 • lvming19901227 视频直播这么火,再不学就out了. 为了紧跟潮流,本文将向大家介绍一下视频直播中的基本流程和主要的技术点,包括但不限于前端技术. 1 H5到底能不能做视频直播? 当然可以, H5火了这么久,涵盖了各个方面的技术. 对于视频录制,可以使用强大的webRTC(Web Real-Time Communication)是一个支持网页浏览器进行实时语音对话或视频对话的技术,缺点是只在PC的chrome

selenium操作下拉滚动条的几种方法

数据采集中,经常遇到动态加载的数据,我们经常使用selenium模拟浏览器操作,需要多次下拉刷新页面才能采集到所有的数据,就此总结了几种selenium操作下拉滚动条的几种方法 我这里演示的是Java版本的,使用chromedriver,当然你可以换成python或其他语言,浏览器用firefox或者phantomjs(无头浏览器),大部分都是适用的,不同浏览器有略微的差异. 初始化一个浏览器 首先要允许浏览器运行js脚本 DesiredCapabilities sCaps = new Desi

selenium对video视频进行识别和操作

redis默认只能localhost登录,所以需要开启远程登录.解决方法如下: 在redis的配置文件redis.conf中,找到bind 127.0.0.1这行 1.注释掉bind 127.0.0.1,将bind 127.0.0.1 改成了bind 0.0.0.0(这样不限制连接的ip,可以任意ip都可以连接此redis) 2.bind  局域网IP    只能局域网内IP的机器访问, 本地localhost都无法访问. 3.如果需要本地与远程都可以登录: 不用注释掉bind 127.0.0.

新版微信h5视频自动播放

微信最近升级了新版本,直播视频不能自动播放,经过了一番探索,发现下列方法可以实现自动播放. if (typeof WeixinJSBridge == "undefined") { document.addEventListener("WeixinJSBridgeReady", function() { setTimeout(function(){ player.play();//调用h5播放器自动播放的方法,我们使用的是video.js播放器. },500); },

ui自动化之selenium操作(五)简单元素操作--续

1. 多窗口切换 有时候需要在多窗口切换,webdriver提供了switch_to_window()方法支持切换窗口: from selenium import webdriver import os,time driver = webdriver.Chrome() frist_url = "http://www.baidu.com" driver.get(frist_url) #获取百度登录窗口句柄 login_windows = driver.current_window_han

Selenium操作页面元素

转自:http://blog.sina.com.cn/s/blog_6966650401012a7q.html 一.输入框(text field or textarea) //找到输入框元素: WebElement element = driver.findElement(By.id("passwd-id")); //将输入框清空: element.clear(); //在输入框中输入内容: element.sendKeys(“test”); //获取输入框的文本内容: element

selenium操作浏览器

package seleniumLearn1; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; /** * 操作浏览器 * 1.浏览器窗口最大化 * 2.浏览器

selenium 操作文本框(textarea输入)

selenium 对文本框的输入操作一般有两种形式,传统的是直接通过定位元素通过sendKeys()方法直接在文本框中输入信息.但有时候我们可以通过id 的方式将其进行定位,但却不能通过sendKeys()向文本框中输入文本信息. 这种情况下,也需要借助JavaScript 代码完成输入. HTML代码 <textarea id="id" style="width: 98%" cols="50" rows="5" cla