title: Selenium WebDriver PageFactory FindsBy Using jQuery Selector?
body: <p>To explain my question, I have given a small scenario:</p>
  
  <p>Say I have a login page.</p>
  
  <pre><code>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()
  
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>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. </p>
  
  <p>Currently, if some things are not always generated on the page until a certain step. I usually do the following:</p>
  
  <pre><code>private const string ThisIsTheUserNameFieldId = "usernamefield";
  </code></pre>
  
  <p>Then hit up the webdriver using the following:</p>
  
  <pre><code>// Navigate to login page
  
  // code here
  
  // Enter in credentials
  
  driver.FindElement(By.Id(ThisIsTheUserNameFieldId)).SendKeys(cred.Username);
  </code></pre>
  
  <p>Not as well structured as the PageFactory, but it's certainly a requirement which I am not able to get around.</p>
  
  <p>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.</p>
  
  <p><a href="http://www.vcskicks.com/selenium-jquery.php" rel="nofollow" title="Selenium jQuery for C#.Net &#40;Including Source&#41;">Selenium jQuery for C#.Net &#40;Including Source&#41;</a></p>
  
  <pre><code>// So I can do things like this:
  driver.FindElement(By.jQuery("a").Find(":contains('Home')").Next())
  </code></pre>
  
  <p><strong>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)?</strong></p>
  
  <pre><code>[FindsBy(How = How.jQuery, Using = "div[id$='txtUserName']")]
  public IWebElement UsernameField { get; set; }
  </code></pre>