英文:
Selenium web driver unable to automatically play video
问题
Sure, here is the translated version of the provided content:
我正在尝试测试我的网站上的视频播放器是否正常工作。
我尝试了以下代码,但视频播放器无法开始播放视频。
driver.get("https://www.w3schools.com/html/html5_video.asp");
//等待页面完全加载
Thread.sleep(10000L);
String videoPlayerPath = "document.getElementById(\"video1\")";
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(videoPlayerPath + ".play()");
在浏览器控制台上,document.getElementById("video1").play()
是有效的,但是我无法通过 Selenium 使其正常工作。
英文:
I'm trying to test if the video player on my website works.
I tried this, but the video player does not start playing the video.
driver.get("https://www.w3schools.com/html/html5_video.asp");
//sleep for the page to entirely load
Thread.sleep(10000L);
String videoPlayerPath = "document.getElementById(\"video1\")";
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript(videoPlayerPath + ".play()");
document.getElementById("video1").play()
works on the browser console, but I'm not able to get it working via Selenium
答案1
得分: 0
使用WebDriverWait
代替Thread.sleep
来加载内容,并且像这样一行代码中使用JS部分:
js.executeScript("document.getElementById('video1').play();");
英文:
use WebDriverWait
instead of Thread.sleep
to load the content and use the JS part in one line like this
js.executeScript("document.getElementById('video1').play();");
专注分享java语言的经验与见解,让所有开发者获益!
评论