user3672802
user3672802

Reputation: 63

Selenium C# How to pause/play an embedded youtube video

So I have this test case in which I open a youtube link which opens a frame with youtube video (embedded) enter image description here

I want to play/pause the video with Selenium. I got an error about no such element. Everything works in other places where I don't have youtube video. This is the code:

    public void PlayMovies()
    {
        driver.FindElement(By.CssSelector("span.movie"), TimeoutToFindElement).Click();
        driver.FindElement(By.CssSelector("li > a.youtubevideo"), TimeoutToFindElement).Click();
        driver.SwitchTo().ActiveElement();


        //driver.FindElement(By.ClassName(".ytp-play-button"), TimeoutToFindElement).Click();
        //driver.FindElement(By.XPath("//div[2]/div/button"), TimeoutToFindElement).Click();
        //driver.FindElement(By.XPath("//*[starts-with(@id,'player_uid')]/div[22]/div[2]/div[1]/button"), TimeoutToFindElement);
        //driver.FindElement(By.CssSelector("button.ytp-play-button.ytp-button"), TimeoutToFindElement).Click();


        driver.FindElement(By.XPath("//div[@id='youtube-modal-container']/div/div/button"), TimeoutToFindElement).Click();
    }

None of those commented lines are working. Also I don't know if SwitchTo has some effects, because I get the same error with it or without.

Upvotes: 0

Views: 1524

Answers (1)

NarendraR
NarendraR

Reputation: 7708

Possibility would be, your video is in iFrame so First you need to switch into that iFrame -

driver.switchTo().frame(0); // 0 represent frame index

and then have to simulate your actions

Upvotes: 1

Related Questions