Search code examples
testingseleniumautomation

How to play & pause video using Selenium?


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

Thanks in advance...


Solution

  • 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.