The objective is to change the HTML video playback rate before the event click
with Selenium.
There are several thread discussing about the request such as OP1 and there were two suggestions;
From the OP1, Jeremy Visser suggested to change the attribute directly, which is as below
document.querySelector('video').defaultPlaybackRate = 2.0;
While Armel on the hand suggested as shown at the code snippet below
var vid = document.getElementById("video1"); function fastPlaySpeed() { vid.playbackRate = 2;}
As suggested by Greg, the approach by Armel can be emulated as below,
from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC chrome_options = webdriver.ChromeOptions() browser = webdriver.Chrome(executable_path=r"Browserschromedriver.exe", options=chrome_options) browser.get("https://www.youtube.com/watch?v=nXbfZL5kttM") WebDriverWait(browser, 70).until(EC.element_to_be_clickable( (By.XPATH, "//button[@aria-label='Play']"))).click() print('Complete play') javascript_to_execute = 'document.getElementById("video1").playbackRate = 2' webdriver.execute_script(javascript_to_execute))
However, it seems that YouTube have different ElementById
for different video hence make the js
approach not working as intended.
Apparently YouTube have specific function: player.setPlaybackRate(suggestedRate:Number):Void
to do this. This function sets the suggested playback rate for the current video iFrame API.
However, I dont have the adequate knowledge on how to integrate setPlaybackRate(suggestedRate:Number)
in my code snippet. Appreciate if someone can shed some light on how to utilize the setPlaybackRate
for this particular case.
Thanks in advance for the time taken entertain this request.
Edit 4:
— Dirty workaround:
a) Install the extension Video Speed Controller
: Only applicable for Chrome browser.
b) Using Default Chrome Profile to load the extension as discussed from OP6, or you can load also via Selenium as discus in detail here.
Edit 3:
This OP4 suggested postMessage on iframe to pass setPlaybackRate command with rate in argument. But still the question is where to place this?
var playbackRate = 2; var data = {event: 'command', func: 'setPlaybackRate', args: [playbackRate, true]}; var message = JSON.stringify(data); $('#iframe1')[0].contentWindow.postMessage(message, '*');
Edit 2:
Apparently YouTube have specific function: player.setPlaybackRate(suggestedRate:Number):Void
to do this. This function sets the suggested playback rate for the current video iFrame API.
Edit 1:
My understanding as commented by Greg Burghardt
WebDriverWait(browser, 70).until(EC.element_to_be_clickable( (By.XPATH, "//button[@aria-label='Play']"))).click() print('Complete play') JavascriptExecutor js; js = (JavascriptExecutor)driver; js.executeScript("vid.playbackRate = 2;");
Advertisement
Answer
Call execute_script on the web driver object:
javascript_to_execute = 'document.getElementById("video1").playbackRate = 2' webdriver.execute_script(javascript_to_execute))