目標是在事件發生前使用 Selenium 更改 HTML 視頻播放速率click。有幾個線程討論了該請求,例如OP1,并且有兩個建議;Jeremy Visser在OP1中建議直接更改屬性,如下document.querySelector('video').defaultPlaybackRate = 2.0;而手上的Armel則建議如下面的代碼片段所示var vid = document.getElementById("video1");function fastPlaySpeed() { vid.playbackRate = 2;}正如Greg所建議的, Armel的方法可以模擬如下,from selenium import webdriverfrom selenium.webdriver.support.ui import WebDriverWaitfrom selenium.webdriver.common.by import Byfrom selenium.webdriver.support import expected_conditions as ECchrome_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))然而,YouTube 似乎對ElementById不同的視頻有不同的規定,因此使該js方法無法按預期工作。顯然 YouTube 有特定的功能:player.setPlaybackRate(suggestedRate:Number):Void做到這一點。此函數設置當前視頻iFrame API的建議播放速率 。但是,我對如何集成setPlaybackRate(suggestedRate:Number)到我的代碼片段中沒有足夠的知識。如果有人能夠闡明如何setPlaybackRate在這種特殊情況下使用它,我將不勝感激。預先感謝您抽出寶貴時間接受此請求。
1 回答

牧羊人nacy
TA貢獻1862條經驗 獲得超7個贊
在 Web 驅動程序對象上調用execute_script:
javascript_to_execute = 'document.getElementById("video1").playbackRate = 2'
webdriver.execute_script(javascript_to_execute))
- 1 回答
- 0 關注
- 175 瀏覽
添加回答
舉報
0/150
提交
取消