我有一个嵌入了视频的 URL,就像 YouTube URL 一样,我想验证视频是否加载和流式传输 我遇到的困难是我没有 tagname 、 id 或视频元素的任何内容,那么在这种情况下我该如何检查
使用 Selenium 的代码:
public class URLCheck
{
public static void main(String args[]){
File file = new File("C:\\Users\\MB0000038\\Desktop\\chromedriver\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
WebDriver driver=new ChromeDriver();
driver.get("https://www.youtube.com/watch?v=FtsrtcagbOQ");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// JavascriptExecutor js = (JavascriptExecutor) driver;
// WebElement video = driver.findElement(By.tagName("video"));
driver.quit();
}
}
此代码运行良好,但仅打开一个显示视频的 Chrome 选项卡
提前致谢
最佳答案
我已检查您的视频,您可以使用两个标签来验证您的视频。
driver.get("https://www.youtube.com/watch?v=59rEMnKWoS4");
driver.manage().window().maximize();
WebElement pauseAndPlay = driver.findElement(By.xpath("//button[@class='ytp-play-button ytp-button']"));
//Get the attribute aria-lable of pauseAndPlay Element which would tells you current state of video(pause/play)
String videoState=pauseAndPlay.getAttribute("aria-label");
if(videoState.equalsIgnoreCase("Pause")){
System.out.println("Video is currently in play state");
Thread.sleep(3000);
//pausing my video and checking the current play time
pauseAndPlay.click();
Thread.sleep(2000);
WebElement currentTimeElement=driver.findElement(By.xpath("//span[@class='ytp-time-current']"));
String currentTime=currentTimeElement.getText();
if(currentTime!="0:00"){
System.out.println("my video is getting progressed and currently at:"+currentTime);
}else{
System.out.println("my video is not getting played");
}
}else if(videoState.equalsIgnoreCase("Play")){
System.out.println("Video is currently paused");
}
关于java - 如何检查 URL 中的视频加载情况?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42897800/