3

I need to get suggestions from YouTube search text box. So far I was doing somthing like below code. But nothing works for me.

I got data on the List, but the "aa" text was

null

            IWebDriver driver  = CodedUITest1.driver;
            driver = new ChromeDriver(@"C:\Driver\");
            // driver = new PhantomJSDriver(@"C:\Driver\");
            // driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
            driver.Navigate().GoToUrl("https://www.youtube.com");
            Thread.Sleep(5000);
            //driver.FindElement(By.Id("logo-icon-container")).Click();
            //Keyboard.SendKeys("{Tab}");
            Keyboard.SendKeys(SearchText);
            Thread.Sleep(3000);
            IWebElement elm = driver.FindElement(By.CssSelector("div.gstl_50.sbdd_a"));
            IList<IWebElement> list = elm.FindElements(By.TagName("div"));
            string aa = list[3].Text;

After some times playing with the code i got 10 items for my list item. But still the string aa is null.

IWebElement elm = driver.FindElement(By.CssSelector("ul.sbsb_b"));
        IList<IWebElement> list = elm.FindElements(By.TagName("b"));
        string aa = list[3].Text;
2

Your locator is wrong. The list items in the search autocomplete have specific classes, so you can find them using the CSS selector .sbsb_c.gsfs.

Secondly, use an explicit wait instead of Thread.Sleep() because you're never sure you will get a response in three seconds.

  • Seems to be Youtube is changing class name of the auto complete text in every 2 hours. – ChathuD Nov 2 '17 at 15:10
  • @ChathuD No they are not. I just checked and it's still the same. – FDM Nov 2 '17 at 18:22
1

i think reason is when you search something in you-tube suggestions are displayed as drop down, if you check that suggestions using firebug or any other inspect element tool you will see all the suggestions are in disable mode.

so i think that the reason your aa result get null value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.