Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4250368
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:31:09+00:00 2026-05-21T04:31:09+00:00

Of late I started dabbling in to C# for Selenium 2.0 (aka WebDriver). For

  • 0

Of late I started dabbling in to C# for Selenium 2.0 (aka WebDriver).
For those who are not aware of Selenium, it lets you control UI objects of web application using html id, name, class etc.

So you can click on an element as –

webDriver.FindElement(By.Id(elementLocator)).Click();

but then you can also use html name or class or XPath to click on element. So it could be –

webDriver.FindElement(By.Name(elementLocator)).Click();
webDriver.FindElement(By.Xpath(elementLocator)).Click();

Soon I realized that I should abstract it in a method and use the method instead of using such long statements through out my tests. So I created a method as –

 public static void click(IWebDriver webDriver, int elementLocatorType, String elementLocator)
    {
        switch (elementLocatorType)
        {
            case 0:
                webDriver.FindElement(By.Id(elementLocator)).Click();
                break;
            case 1:
                webDriver.FindElement(By.Name(elementLocator)).Click();
                break;
            case 2:
                webDriver.FindElement(By.XPath(elementLocator)).Click();
                break;
        }

    }

and use it as –

Commons.click(webDriver, 0, elementLocator)

I am wondering if I could improve it any more, say if I could avoid switch statement and use something better….

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:31:10+00:00Added an answer on May 21, 2026 at 4:31 am

    The least you should do is to introduce an enumeration for that elementLocatorType.

    But I would do it like this:

    public static void ClickElement(this IWebDriver webDriver, Element element)
    {
        webDriver.FindElement(element).Click();
    }
    
    public static void ClickElementById(this IWebDriver webDriver, string elementLocator)
    {
        webDriver.ClickElement(By.Id(elementLocator));
    }
    
    public static void ClickElementByName(this IWebDriver webDriver, string elementLocator)
    {
        webDriver.ClickElement(By.Name(elementLocator));
    }
    
    public static void ClickElementByXPath(this IWebDriver webDriver, string elementLocator)
    {
        webDriver.ClickElement(By.XPath(elementLocator));
    }
    

    You can use it like this:

    webDriver.ClickElementById(elementLocator1);
    webDriver.ClickElementByName(elementLocator2);
    webDriver.ClickElementByXPath(elementLocator3);
    

    If there are even more possibilities to locate an element, consider passing the location method as a delegate as Florian’s answer suggests.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our web application started out as a big, honkin' ASP.NET AJAX 'page' with oodles
Of late I have been diving deep into web application security. While browsing I
it's late, I'm tired, and probably being quite dense.... I have written an application
So I started using Git a few days ago. ( Very late to the
Following on from some comments of late, I have took the plunge and started
Since the datastructure of my application domain is becoming pretty complex as of late,
It must be late, but i'm not seeing the errors of my ways. i'm
We are developing a web application that will be sold to many clients. There
I'm working on analyzing the performance of a web application which runs for now
Here's my situation. I've just started a new job and I've inherited an application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.