Sunday 16 March 2014

How to select the value from the web list with selenium webdriver in C#.Net?

We can select the value from the dropdown or combobox on the webpage using 3 methods

1.      SelectByText method   - selects by the text displayed in the combo box
2.      SelectByIndex method  - selects element by position
3.      SelectByValue method  - selects element by the value of the option 

You can use any of these methods to select a value from the dropdown.

Complete example in C#.Net is given below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using OpenQA.Selenium;

using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Support.UI;
using System.Collections.ObjectModel;
namespace Abc
{
    class Program
    {
        static void Main(string[] args)
        {

           
IWebDriver driver=null;
try
{
driver = new ChromeDriver(@"F:\selenium\csharp");
driver.Url = "http://www.amazon.in";

              driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(20));

driver.Navigate();

//enter data in amazon search box

IwebElement e = driver.FindElement(By.XPath ("//*[@id='searchDropdownBox']"));

SelectElement select=new SelectElement(e);
//select the value Books from the combo box
select.selectByText("Books");  


            }

 catch(Exception e){
 Console.WriteLine("Exception ******"+e.ToString());
               
 }
           
            finally{
            Thread.Sleep(2000);
            driver.Quit();
            Console.ReadLine();
            }

           
        }
    }

}

What do you think on above selenium topic. Please provide your inputs and comments. You can write to me at reply2sagar@gmail.com

No comments:

Post a Comment

Buy Best Selenium Books

Contributors