c# - Selenium C# 如何暂停/播放嵌入式 youtube 视频

标签 c# selenium testing youtube automated-tests

所以我有这个测试用例,我在其中打开了一个 youtube 链接,该链接打开了一个带有 youtube 视频(嵌入式)的框架 enter image description here

我想用 Selenium 播放/暂停视频。我收到关于没有这样的元素的错误。在我没有 youtube 视频的其他地方一切正常。 这是代码:

    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();
    }

这些注释行都不起作用。另外我不知道 SwitchTo 是否有一些效果,因为无论有没有我都会遇到同样的错误。

最佳答案

可能是,您的视频在 iFrame 中,所以首先您需要切换到那个 iFrame -

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

然后还要模拟你的 Action

关于c# - Selenium C# 如何暂停/播放嵌入式 youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41415459/

相关文章:

c# - 该构建还原了 NuGet 包。再次构建项目以将这些包包含在构建中

c# - 在 Windows 7 中运行应用程序的证书问题

c# - Noda 时间单元测试 XML 错误

java - 无法使用 selenium sendKeys 在文本字段中输入字符

java - 如何使用 Selenium、Java 从 div 元素中获取文本?

用于创建 stub /假对象的 Python 库

testing - 从终端进行 Silverstripe 测试

unit-testing - Visual Studio Live Testing - 启动失败,因为无法找到 TestPlatform V2

c# - 异步调用带有输出参数的方法

html - 使用 CSS 以适合 Selenium 的样式定位元素?