使用带有 Java 的 Selenium WebDriver 播放视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17627554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:30:36  来源:igfitidea点击:

Play the video using Selenium WebDriver with Java

javatestingselenium-webdriver

提问by user2467785

I am trying to play the video (Using JUnit) - Day01 of following website. http://www.itelearn.com/live-training/security-testing-live-trainingWhat I am trying to achieve is after playing the video I will take a screen shot to prove that video is playing properly. After clicking on Day01 video, it opens in a new window- when I looked at the code I realized that they have used iFrame. I am able to close this video window but not able to play/pause this video.

我正在尝试播放视频(使用 JUnit) - 以下网站的 Day01。 http://www.itelearn.com/live-training/security-testing-live-training我想要实现的是在播放视频后我将截屏以证明视频正在正常播放。单击 Day01 视频后,它会在一个新窗口中打开 - 当我查看代码时,我意识到他们使用了 iFrame。我可以关闭此视频窗口,但无法播放/暂停此视频。

To close the video I have used code-- WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xPath))).click();

要关闭视频,我使用了代码——WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xPath))).click();

I am new to testing please help me.

我是测试新手,请帮助我。

回答by Yi Zeng

So did you switch into the iframe?

所以你切换到iframe了吗?

Try the following untested Java code, please note that the logic here is simple, you find the iframe element by xpath or css selector, then switch to it, then click. However, automating player may not be that easy and stable. Provide feedback if you can, thanks.

试试下面未经测试的Java代码,请注意这里的逻辑很简单,你通过xpath或css选择器找到iframe元素,然后切换到它,然后点击。然而,自动化播放器可能并不那么容易和稳定。如果可以,请提供反馈,谢谢。

WebDriverWait wait = new WebDriverWait(driver, 20);
WebElement playerIframe = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#sb-player iframe")));
driver.switchTo().frame(playerIframe);

// make sure you have html5 video loaded, instead of flash
// otherwise Selenium won't find a thing
driver.findElement(By.cssSelector("svg.ytp-cued-icon")).click();

To load HTML5 by default, please see this pageor load extension HTML5 Video for YouTubewhen starting Selenium.

要默认加载 HTML5,请在启动 Selenium 时查看此页面或加载YouTube 的扩展HTML5 视频

回答by Konstantin Yakovskyi

Try to use JavaScriptExecutor. The following approach works for me:

尝试使用 JavaScriptExecutor。以下方法对我有用:

import org.openqa.selenium.JavascriptExecutor;

JavascriptExecutor js = (JavascriptExecutor) driver;
js .executeScript("document.getElementById(\"video\").play()");

I was able to play video from https://www.w3.org/2010/05/video/mediaevents.htmland verify Progress field.

我能够从https://www.w3.org/2010/05/video/mediaevents.html播放视频并验证 Progress 字段。

Good luck!

祝你好运!

回答by Hukmaram

WebDriver driver=new FirefoxDriver();
driver.get("https://www.wonderplugin.com/wordpress-lightbox");
WebElement element=driver.findElement(By.xpath("//a[contains(text(),'Open a Div in Lightbox')]"));
element.click();
WebElement frameElement=driver.findElement(By.xpath("//iframe[@src='https://www.youtube.com/embed/wswxQ3mhwqQ']"));
        driver.switchTo().frame(frameElement);
        driver.findElement(By.xpath("//button[@aria-label=\'Play\']")).click();