Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SeleniumWebDriver和Java播放视频_Java_Testing_Selenium Webdriver - Fatal编程技术网

使用SeleniumWebDriver和Java播放视频

使用SeleniumWebDriver和Java播放视频,java,testing,selenium-webdriver,Java,Testing,Selenium Webdriver,我正在尝试播放以下网站的视频(使用JUnit)-Day01。 我试图实现的是,在播放视频后,我将拍摄一张屏幕截图,以证明视频播放正确。点击Day01视频后,它会在一个新窗口中打开——当我查看代码时,我意识到他们使用了iFrame。我可以关闭此视频窗口,但无法播放/暂停此视频 要关闭视频,我使用了以下代码-- WebDriverWait wait=新的WebDriverWait(驱动程序,20); wait.until(ExpectedConditions.visibilityOfElementL

我正在尝试播放以下网站的视频(使用JUnit)-Day01。 我试图实现的是,在播放视频后,我将拍摄一张屏幕截图,以证明视频播放正确。点击Day01视频后,它会在一个新窗口中打开——当我查看代码时,我意识到他们使用了iFrame。我可以关闭此视频窗口,但无法播放/暂停此视频

要关闭视频,我使用了以下代码-- WebDriverWait wait=新的WebDriverWait(驱动程序,20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath))).click()


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

那么您是否切换到iframe

尝试以下未经测试的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();

要默认加载HTML5,请在启动Selenium时查看或加载扩展。

尝试使用JavaScriptExecutor。以下方法适用于我:

import org.openqa.selenium.JavascriptExecutor;

JavascriptExecutor js = (JavascriptExecutor) driver;
js .executeScript("document.getElementById(\"video\").play()");
我能够播放视频,并验证现场进度


祝你好运

谢谢你的回复。我试过这个代码,但不起作用。其返回类型eis无效时,其行中的给定错误。WebElement PlayerFrame=wait.until(ExpectedConditions.visibilityOfElementLocated(由.cssSelector(“#sb player iframe”)))。单击();是的,这只是一个简单的语法错误,因为我没有Java环境。你肯定能修好你自己。只需删除结尾
click()
,因为我从您的原始帖子中复制了这一行,但忘记了删除。非常感谢您的帮助,我对此表示感谢。我将下载HTML5视频播放器并尝试此代码。在此期间,我尝试了以下代码。所以现在我可以看到播放按钮是红色的,这意味着我的控制/光标在该视频上,但当我尝试单击它时,它不会播放视频。WebElement location=driver.findElement(By.xpath(xpath))//获取成员区域xpath Actions builder=新操作(驱动程序);builder.moveToElement(location.build();//builder.contextClick();builder.contextClick().perform();等待时间(10000);你能解释一下这段代码是做什么的,以及它是如何回答最初的问题的吗?它将打开一个帧并在该帧中运行视频
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();