How to select date from calendar in selenium webdriver using c#

How to select a date from a calendar in Selenium WebDriver using C#

In order to select a date from a calendar using Selenium WebDriver with C#, you can follow these steps:

  1. Launch the WebDriver and navigate to the desired webpage.
  2. Identify and locate the calendar element on the webpage using appropriate locators such as ID, class, XPath, etc.
  3. Click on the calendar element to open the date picker or calendar popup.
  4. Identify and locate the specific date element on the calendar popup.
  5. Click on the desired date element to select it.

Let’s consider an example to illustrate the steps mentioned above:

    using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

class CalendarExample
{
    static void Main()
    {
        // 1. Launch the WebDriver and navigate to the webpage
        IWebDriver driver = new ChromeDriver();
        driver.Navigate().GoToUrl("https://example.com");

        // 2. Identify and locate the calendar element
        IWebElement calendarElement = driver.FindElement(By.Id("calendar"));

        // 3. Click on the calendar element to open the date picker
        calendarElement.Click();

        // 4. Identify and locate the specific date element
        IWebElement dateElement = driver.FindElement(By.XPath("//td[@data-date='2022-01-10']"));

        // 5. Click on the desired date element to select it
        dateElement.Click();
    }
}
  

In the above example:

  • We launch the ChromeDriver and navigate to the desired webpage using the Navigate().GoToUrl() method.
  • We locate the calendar element on the page using the FindElement() method, which is identified by its id attribute.
  • We click on the calendar element using the Click() method to open the date picker or calendar popup.
  • We locate the desired date element on the calendar popup using the FindElement() method and XPath expression.
  • Finally, we click on the desired date element using the Click() method to select it.

Remember to download and add the necessary Selenium WebDriver and Selenium WebDriver ChromeDriver packages to your project.

Hope this helps! Let me know if you have any further questions.

Leave a comment