Friday, December 7, 2018

Selenium from a different perspective - Google News reading bot


  1. From the below code user will navigate to the news.google.com.
  2. Extract everything under span tag.
  3. Save the extracted text to a string array.
  4. Data cleansing on the array.
  5. Transfer the array text to speech engine. 



public static void Main(string[] args)
        {

            IWebDriver driver;
            driver = new ChromeDriver(@"C:\Driver\2.41.0\");
            driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
            driver.Navigate().GoToUrl("https://news.google.com");

            IList<IWebElement> elm = driver.FindElements(By.TagName("span"));

            string[] a = new string[2000];
            int i = 0;
            foreach (var item in elm)
            {
                a[i++] = item.Text.ToString();
            }
            a = a.Where(x => !string.IsNullOrEmpty(x)).ToArray();

            int j = 0;
            foreach (var item in a)
            {
                if (a[j].Length < 30)
                {
                    a[j] = "";
                }
                j++;
            }
            a = a.Where(x => !string.IsNullOrEmpty(x)).ToArray();

            SpeechSynthesizer synthesizer = new SpeechSynthesizer();
            synthesizer.SelectVoiceByHints(VoiceGender.Female, VoiceAge.Senior);
            synthesizer.Volume = 100;
            synthesizer.Rate = -1;
            int k = 1;
            foreach (var item in a)
            {
                synthesizer.Speak(a[k]);
                k++;
            }
        }