使用C#查找视频是否可用使用Selenium

时间:2018-03-25 18:35:22

标签: c# selenium-webdriver youtube youtube-api selenium-chromedriver

如何使用C#使用Selenium查找视频是否可用?

IWebDriver driver = new ChromeDriver(@"C:\chromedriver_win32");
driver.Navigate().GoToUrl("https://www.youtube.com/watch?v=v19-La3pTmU");

var result = driver.FindElement(By.ClassName("reason style-scope ytd-player-error-message-renderer")).Text;

这是我的代码段,用于查找youtube视频是否可用。如果没有,我可以访问文本为"此视频不可用。"如下图所示,但我无法使用我的代码段来访问此文本。

enter image description here

1 个答案:

答案 0 :(得分:1)

To extract the text This video is unavailable. you can use either of the following lines of code :

  • XPath :

    var result = driver.FindElement(By.XPath("//div[@class='reason style-scope ytd-player-error-message-renderer']")).GetAttribute("innerHTML");
    
  • CssSelector :

    var result = driver.FindElement(By.CssSelector("div.reason.style-scope.ytd-player-error-message-renderer")).GetAttribute("innerHTML");
    

Perhaps you may require to induce WebDriverWait in conjunction with ExpectedConditions as follows :

  • XPath :

    wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
    var result = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("//div[@class='reason style-scope ytd-player-error-message-renderer']"))).GetAttribute("innerHTML");
    
  • CssSelector :

    wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
    var result = wait.Until(ExpectedConditions.ElementIsVisible(By.XPath("div.reason.style-scope.ytd-player-error-message-renderer"))).GetAttribute("innerHTML");