[Edit: resolved, see middle in question text]
A quick question about understanding the contents of a Youtube page with playing videos in it:
Finally
I use Selenium to play videos on Youtube as part of a browser-based GUI.
I need to know when the video finished playing, as the GUI does something else (for example, submit local HTML in a browser).
Code snippet and question
import os, time from selenium import webdriver
At this point, I could time.wait() for the length of the video.
However, I am wondering if I can request the youtube page through the selenium driver and measure the time remaining to play in the while loop (I don't know how to extract this information from the YouTube page)
Thanks!
[Edits with solution]
Selenium solution
Thanks to Stanjer and this answer and this other answer you can get movie_player through this method:
player_status = driver.execute_script("return document.getElementById('movie_player').getPlayerState()")
(don't forget to add "return" at the beginning of the script)
Selenium Alternative Solution
Less elegant, but worth noting: driver.text returns a string representing the video timer in this string format '1: 00/2: 00'. So you can check if the video has played something in this direction:
video_is_playing = True while video_is_playing: time.sleep(1) video_is_playing = not(driver.text[:4] == driver.text[-4:])
[edit] According to Hos's comment, this information can also be accessed:
driver.find_element_by_class_name("ytp-time-current").text
Complication and the next question
I need to open the video in maximum format and with auto play.
This means that I am invoking the following URL:
youtube.com/v/<video_code>?rel=0&autoplay=1
However, this returns a very short html that contains only embed code, for example, here:
<HTML><HEAD></HEAD> <BODY leftMargin=0 scroll=no topMargin=0> <EMBED height="100%" type=application/x-shockwave-flash width="100%" src=https://www.youtube.com/v/Fsc-oT9PsSQ?rel=0&autoplay=1 fullscreen="yes"> </BODY> </HTML>
So, I do not have the movie_player element.
Approach 1 - can I extract the timer from application/x-shockwave-flash ?
Approach 2 - If I run a YouTube video on a classic Youtube page, how can I tell movie_player to maximize myself?
(note: this answer and this answer probably contains information for solving approach 2, will post if I get this to work with Selenium)