所以我有这个测试用例,我在其中打开了一个 youtube 链接,该链接打开了一个带有 youtube 视频(嵌入式)的框架
我想用 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/