Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get Youtube title using Selenium C#

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;

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading