Home > Software engineering >  In selenium how to pause a YouTube video embedded inside i frame?
In selenium how to pause a YouTube video embedded inside i frame?

Time:01-11

I am trying to play a video & then pause it , the element is located inside a iframe I am able to play the video how can I pause the same , I am using button action to play but this is not available once video is playing

driver.get('https://demo.guru99.com/test/guru99home/')
frame1 = driver.find_element(By.XPATH,"//iframe[@src='https://www.youtube.com/embed/RbSlW8jZFe8']")
driver.switch_to.frame(frame1)

sleep(2)
Ytbutton = driver.find_element(
    By.XPATH, "//button[@class='ytp-large-play-button ytp-button ytp-large-play-button-red-bg']")
Ytbutton.click() #play
sleep(2)
# for pause ?

CodePudding user response:

This is because upon pressing the play button, the button will be hidden afterward.

The easiest way without tinkering with hidden element would be simply utilizing ActionChain to invoke a space bar send_keys(' ') or k send_keys('k') command which is shortcut for Youtube player to pause and unpause a video.

Something like following :

from selenium.webdriver import ActionChains

driver.get('https://demo.guru99.com/test/guru99home/')
frame1 = driver.find_element(By.XPATH,"//iframe[@src='https://www.youtube.com/embed/RbSlW8jZFe8']")
driver.switch_to.frame(frame1)

sleep(2)
Ytbutton = driver.find_element(
    By.XPATH, "//button[@class='ytp-large-play-button ytp-button ytp-large-play-button-red-bg']")
Ytbutton.click() #play
sleep(2)
ActionChains(driver).send_keys('k').perform() #pause

CodePudding user response:

If you dig below in the iframe, you can find another changeable button:

pause_button = driver.find_element(
    By.XPATH, "//button[@class='ytp-play-button ytp-button']")
pause_button.click()

It, at the end of the video, turns into a "repeat". It is in the <div > block.

  • Related