I am building a testing framework, my objective is to test many websites that have similar pages with light difference between each other using this framework.

I have an issue where I want WebElements Selectors to be dynamic, which means that I want to pass the way I want to find the element as a parameter to FindElement method.

I am trying to build something like this:

public class WebComponent
            public int ID { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public IWebElement WebElement{get;set;}
            public Accessor Accessor { get; set; }
            public WebComponent()
                Accessor = new Accessor();

public class Accessor
    OpenQA.Selenium.By By { get; set; }
    public string Value { get; set; }

And later in my code when I want to have instance of this class:

WebComponent component = new WebComponent();
component.ID = 1;
component.Name = "Logout Button";
component.Description = "The button to click when user wants to logout of website";
component.Accessor.By = By.Id;
component.Accessor.Value = "logout";
component.WebElement = Browser.Driver.FindElement(//missing code);

My question is how can I find the WebElement using component.Accessor, any advice or suggested edits will be much appreciated.

1 Answer 1


Advice would be not to split Value and By in your Accessor class. Id is not a property of By class of selenium. Id is a method with parameters, so you can't invoke it as you did:

component.Accessor.By = By.Id;

Connect value and By in Accessor class - you don't have to complicate things that doesn't need to be complicate.

Your accessor class could look like this:

public class Accessor
    public By By { get; set;}

And then you would initialize By like that:

Accessor.By = By.Id("logout");

And then your locating method part could look like this:

component.WebElement = Browser.Driver.FindElement(component.Accessor.By);

If you still want to stick with your way - try reflection. Some ideas could be found here: stackoverflow link to similar question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.