Home > Back-end >  Get Youtube title using Selenium C#
Get Youtube title using Selenium C#

Time:09-08

Hi I am trying to use C# selenium to get a youtube video title. These won't work. I already tried getting .Text from other elements and they work fine. So I narrowed down to these lines which I think is problematic. Help is very much appreciated. Will love to know where I went wrong.

var title = driver.FindElement(By.Id("title")).FindElement(By.ClassName("style-scope ytd-watch-metadata")).Text;
var title = driver.FindElement(By.CssSelector("h1.title yt-formatted-string")).Text;

CodePudding user response:

Your locators are not unique.
Try this:

var title = driver.FindElement(By.CssSelector("#info-contents h1.title yt-formatted-string")).Text;

Also style-scope ytd-watch-metadata are actually 2 class names: style-scope and ytd-watch-metadata while By.ClassName accepts single class name parameter. For multiple class names use XPath or CSS Selectors.
Also don't forget to add delays, preferably WebDriverWait expected conditions explicit waits.

  • Related