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 6914881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:24:16+00:00 2026-05-27T09:24:16+00:00

To explain my question, I have given a small scenario: Say I have a

  • 0

To explain my question, I have given a small scenario:

Say I have a login page.

public class LoginPage
{
    [FindsBy(How = How.Id, Using = "SomeReallyLongIdBecauseOfAspNetControlsAndPanels_username"]
    public IWebElement UsernameField { get; set; }

    [FindsBy(How = How.Id, Using = "SomeReallyLongIdBecauseOfAspNetControlsAndPanels_password"]
    public IWebElement PasswordField { get; set; }

    [FindsBy(How = How.Id, Using = "submitButtonId")]
    public IWebElement SubmitButton { get; set; }

    private readonly IWebDriver driver;

    public LoginPage(IWebDriver driver)
    {
        this.driver = driver;

        if(!driver.Url.Contains("Login.aspx"))
        {
            throw new NotFoundException("This is not the login page.");
        }
        PageFactory.InitElements(driver, this);
    }

    public HomePage Login(Credentials cred)
    {

       UsernameField.sendKeys(cred.Username);
       PasswordField.SendKeys(cred.Password);
       SubmitButton.Click();

       return new HomePage(driver);
    }

}

[TestFixture]
public class Test : TestBase
{
    private IWebDriver driver;

    [SetUp]
    public void SetUp()
    {

       driver = StartDriver(); // some function which returns my driver in a wrapped event or something so I can log everything it does.
    }

    [Test]
    public void Test()
    {
        new LoginPage(driver)
                .Login(new Credentials 
                           { Username = "username", 
                             Password = "password" })
                .SomeHomePageFunction()

    }

Eventually, I know the page configuration will change, the id’s will mostly stay the same, but things are changing rapidly on my projects. I know xPath is another alternative, but due to how the pages are generated based on certain critera, this will still become painful as the path will not always be the same.

With the current code above, the page is loaded and the PageFactory init’s the elements through the Page Constructor. All great. This is what I use at the moment.

Currently, if some things are not always generated on the page until a certain step. I usually do the following:

private const string ThisIsTheUserNameFieldId = "usernamefield";

Then hit up the webdriver using the following:

// Navigate to login page

// code here

// Enter in credentials

driver.FindElement(By.Id(ThisIsTheUserNameFieldId)).SendKeys(cred.Username);

Not as well structured as the PageFactory, but it’s certainly a requirement which I am not able to get around.

I have recently come across some jQuery Selector code to use with C#.Net which extends the functionality of the RemoteWebDriver where I can use jQuery selectors to find my Elements on the page.

Selenium jQuery for C#.Net (Including Source)

// So I can do things like this:
driver.FindElement(By.jQuery("a").Find(":contains('Home')").Next())

Does anyone know how I can extend the [FindsBy] Attribute in Selenium WebDriver so that it’s possible to use something like the following (pseudo code)?

[FindsBy(How = How.jQuery, Using = "div[id$='txtUserName']")]
public IWebElement UsernameField { get; set; }
  • 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-27T09:24:16+00:00Added an answer on May 27, 2026 at 9:24 am

    This doesn’t extend [FindsBy], but did you know you can use elements returned by javascript?:

    var driver = new FirefoxDriver { Url = "http://www.google.com" };
    var element = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript("return document.getElementsByName('q')[0];");
    element.SendKeys("hello world");
    

    You could easily extend this to allow for jquery selectors by first injecting jquery (taken from JQuerify and modified):

    const string js =
         @"{var b=document.getElementsByTagName('body')[0]; if(typeof jQuery=='undefined'){var script=document" +
         @".createElement('script'); script.src='http://code.jquery.com/jquery-latest.min.js';var head=document" +
         @".getElementsByTagName('head')[0],done=false;script.onload=script.onreadystatechange=function(){if(!" +
         @"done&&(!this.readyState||this.readyState=='loaded'||this.readyState=='complete')){done=true;script." +
         @"onload=script.onreadystatechange=null;head.removeChild(script);}};head.appendChild(script);}}";
    ((IJavaScriptExecutor)driver).ExecuteScript(js);
    

    And then running javascript to select the element you want:

    var driver = new FirefoxDriver { Url = "http://www.google.com" };
    var element = (IWebElement)((IJavaScriptExecutor)driver).ExecuteScript(@"return $('input[name*=""q""]')[0];");
    element.SendKeys("hello world");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two examples I have a question about. Let me explain via some
With my English is bad, but I'll try to explain. I have a question.
Let me use the following example to explain my question: public string ExampleFunction(string Variable)
First thing i want to say that it's not an easy question to explain,
I will start with the question and then proceed to explain the need: Given
I have got just a quick question regarding the header hierarchy when using HTML5
I have the following classes. public class B { public A a; public B()
the question explains it all. For instance, if I have a NSDictionary like this
This is a very hard to explain question and I hope my code extract
Bear with me while I explain my question. Skip down to the bold heading

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.