2

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"\Browsers\chromedriver.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;");
mpx
  • 3,081
  • 2
  • 26
  • 56
  • Does this answer your question? [How to use JavaScript with Selenium WebDriver Java](https://stackoverflow.com/questions/11430773/how-to-use-javascript-with-selenium-webdriver-java) – Greg Burghardt Apr 19 '20 at 13:34
  • Hi @GregBurghardt, thanks for the prompt reply but the thread you mentioned above does not explicitly address about the playback rate problem. – mpx Apr 19 '20 at 13:37
  • Simply replace the JavaScript the code in `ExecuteScript(...)` with the code you need to alter the playback rate. – Greg Burghardt Apr 19 '20 at 13:43
  • Hi @Greg, then where should I put the java script? on its own file or inside the main Python file? – mpx Apr 19 '20 at 13:50
  • As a string argument to the executeScript method. – Greg Burghardt Apr 19 '20 at 13:54
  • Hi @GregBurghardt, are you suggesting something like the Edit 1, i made above? This changes was made according to the suggestion from Petr Janeček: https://stackoverflow.com/a/11439939/6446053 – mpx Apr 19 '20 at 14:00
  • I'll post an answer. Also, you wrote Java. The Python and Java APIs are the same, but be sure to translate the programing languages. – Greg Burghardt Apr 19 '20 at 14:04

1 Answers1

0

Call execute_script on the web driver object:

javascript_to_execute = 'document.getElementById("video1").playbackRate = 2'
webdriver.execute_script(javascript_to_execute))
Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92
  • Thanks for the reply @Greg. Just to confirm, the first line is the Java script is it? And say in other application where I have a js that is longer than 1 line, should I wrap all the js syntax inside the double quotes also? Anyhow, thanks for replying. And I believe my question in this comment required other thread. I have to play around with your suggestion as YouTube does not the ID element with name video1. – mpx Apr 19 '20 at 14:14
  • @balandongiv: I updated my answer. Hope this clarifies things. – Greg Burghardt Apr 19 '20 at 14:28
  • Hi Greg, thanks for the new suggestion, and sorry for the late response. But I still unable to change the playback rate as per your suggestion. – mpx Apr 23 '20 at 14:23