Saturday, 22 August 2015

C# Webdriver - Fetch all visible URL's using 'IEnumerable' Select method.

Fetch all visible URL's using 'IEnumerable' Select method.
using System;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using System.Threading;
using System.Collections.Generic;
using System.Linq;

namespace TestConsoleApplication
{
    class Program
    {
        static void Main(string[] args) 
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("http://www.google.com");

            driver.FindElement(By.CssSelector("#lst-ib")).SendKeys("Webdriver");
            driver.FindElement(By.CssSelector("#lst-ib")).SendKeys(Keys.Return);
            Thread.Sleep(2000);

            var list = driver.FindElements(By.CssSelector("._Rm")).Select(x => x.Text);

            foreach(var text in list){
                Console.WriteLine(text);
            }
             
            Console.WriteLine("------------------------------------");
            Console.ReadLine();

            driver.Close();
            driver.Quit();
            driver.Dispose();
        }
 
    }
}

Fetch all visible URL's without using 'IEnumerable' Select method.
using System;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium;
using System.Threading;

namespace TestConsoleApplication
{
    class Program1
    {
        static void Main(string[] args)
        {
            IWebDriver driver = new FirefoxDriver();
            driver.Manage().Window.Maximize();
            driver.Navigate().GoToUrl("http://www.google.com");

            driver.FindElement(By.CssSelector("#lst-ib")).SendKeys("Webdriver");
            driver.FindElement(By.CssSelector("#lst-ib")).SendKeys(Keys.Return);
            Thread.Sleep(2000);

            var webElements = driver.FindElements(By.CssSelector("._Rm"));

            foreach (var webElement in webElements)
            {
                Console.WriteLine(webElement.Text);
            }

            Console.WriteLine("------------------------------------");
            Console.ReadLine();

            driver.Close();
            driver.Quit();
            driver.Dispose();
        }
    }
}


No comments:

Post a Comment