How to play & pause video using Selenium?

2019-04-12 16:18发布

问题:

Can anyone help me with how to automate play/pause video present on the web page using Selenium..

Thanks in advance...

回答1:

This is extremely dependent on the browser and the player handling the video. You'll need to use the JavaScript executor, most likely.

I was discussing this last night with a pal and he came up with the following example using the Python variant of Webdriver for a demo video from html5demos.com:

driver = webdriver.Firefox()
driver.get("http://html5demos.com/video")
driver.execute_script('document.getElementsByTagName("video")[0].play()')

You can also "pause" where the "play" is used.

There's a larger question here: what are you actually trying to verify? That simply playing and pausing doesn't throw any errors? Make sure you know what you're validating and that it makes sense to actually work to automate the video test versus just leaving that particular use case to a manual test. (Though you could use the above script to get you to that point!)

** EDIT: Check out this bit of Python code (not mine) which exposes a "Paused" property. Now you could at least validate video loads, starts, and can be stopped. I'm still skeptical of the use of this sort of test, but at least it's a start.



回答2:

By Using FlashObjectwebDriver !

Now what is FlashObjectWebDriver ?

FlashObjectWebDriver is an interface under Webdriver Library. FlashObjectWebDriver has a Method called : callFlashObject callFlashObject method can be over loaded using arguments ie. callFlashObject(“Play”) : For Playing the Flash callFlashObject(“Pause”) : For Pausing the Flash callFlashObject(“Previous”) : For playing previous Flash video callFlashObject(“next”) : For playing next Flash video callFlashObject(“SetVariable”, “/:Message) : For displaying the message . Implementation :

FlashObjectWebDriver flashApp = new FlashObjectWebDriver(driver, "myFlashMovie");       
    // Pass the URL of video        
    driver.get("http://demo.guru99.com/flash-testing.html");            
    Thread.sleep(5000);     
    flashApp.callFlashObject("Play");           
    Thread.sleep(5000);     
    flashApp.callFlashObject("StopPlay");           
    Thread.sleep(5000);     
    flashApp.callFlashObject("SetVariable","/:message","Flash testing using selenium Webdriver");
    System.out.println(flashApp.callFlashObject("GetVariable","/:message"));