watir

I tried to stop and start this video of youtube using watir but resume is not working


I tried to stop and start this video of youtube. At first, I am able to press the play button successfully, but and pause button click works successfully too but I tried to resume the video again by pressing the play button but it's not working. Here is the program

require 'watir'

# Open the URL in a new browser window
browser = Watir::Browser.new :chrome
browser.goto 'https://www.youtube.com/watch?v=6bsggpb5f5g'
browser.window.maximize
sleep 2
browser.button(title: 'Play (k)').click #it's working

# Wait for 10 minutes
sleep(10)

# Click the pause button
browser.element(title: 'Pause (k)').click #It's working
sleep 5

browser.button(title: 'Play (k)').click #It's not working here.

Solution

  • I tried your code and only reason i see for Play (k) button to be unavailable is hovering on it. If your cursor is on top off the play button, it becames unclickable.

    You need to move your cursor away, or just scroll the page a little and it will become clickable again. For scrolling i did:

    browser.div(id: "player").scroll.to(:top) unless browser.button(title: 'Play (k)').exists?
    

    Note: it can also be the same situation for pause. its better to add the same control before the click actions.

    And be carefull there might be an add pop before the video start.