title: Is it possible to get the unique selector from an IWebElement (WebElement) in Selenium using C#
body: <p>tldr; version here:  I would like to retrieve a unique selector, or create one, from an IWebElement.  Is this possible?</p>
  
  <p>I've got a testing framework where I've created actor classes for various elements such as an input box with a label.  The class is generalized so that a person doesn't need to create a new "click" method each time an input with a label is encountered.</p>
  
  <p>My class has among its properties the IWebDriver and a few By selectors.  Of course, when it needs to take an action, it combines the two into a sensible command. Here is a simple example for my class.</p>
  
  <pre><code>public class InputActorClass
  {
      IWebDriver Driver {get; set;}
      By Input {get; set;}
      public InputActorClass(IWebDriver driver, By selector)
      {
          Driver = driver;
          Selector = selector;
      }
      public InputActorClass SelectCheckbox()
      {
          IWebElement actor = Driver.FindElement(Input);
          if (!actor.selected) { actor.click(); }
          return this;
      }
  }
  </code></pre>
  
  <p>As you can see, I just need to call SelectCheckbox and the class already knows the has the By selector object and the IWebDriver object.</p>
  
  <p>However, I have come across a situation where I have to scan a list of input elements to find the one with the correct text.  But the only way I can think to do that at the moment is to do the following:</p>
  
  <pre><code>public void SelectOption(string value)
  {
      By itemselectors = new ByChained(container, By.CssSelector("input"));
      foreach (IWebElement el in driver.FindElements(itemselectors))
      {
          if (el.text.Equals(value))
              actor = new InputActorClass(driver, el.???);
      }
      actor.SelectCheckbox();
      // and do other stuff that the InputActorClass may provide.
  }
  </code></pre>
  
  <p>An astute reader might suggest I use XPath to create my selector instead of going through the whole foreach loop. My problem is that these particular input elements are nested in various dynamically created categories and may have the same name between the categories.  And, since it is dynamically created, I'm not guaranteed my desired element is in the same nth-child() location.  </p>
  
  <p>Of course, I may just have to break down and use XPath to select the category and the input element. I'm hoping there is a way to do what I'm asking.</p>
  
  <p>Here's some html for example:</p>
  
  <pre><code>&lt;body&gt;
    &lt;div class="checkbox"&gt;
      &lt;div&gt;
        &lt;input type="checkbox"/&gt;
        &lt;label&gt;My First Checkbox&lt;/label&gt;
      &lt;/div&gt;
      &lt;div&gt;
        &lt;input type="checkbox"/&gt;
        &lt;label&gt;My Second Checkbox&lt;/label&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/body&gt;
  </code></pre>
  
  <p>Here's some C# code that might iterate through the html:</p>
  
  <pre><code>CheckboxActorClass InputActor;
  foreach (IWebElement el in driver.FindElements(By.CssSelector(".checkbox label")))
  {
      if (el.Text.Equals(textToMatch))
      {
          By selector = el.GetUniqueSelector();  // this line is obviously not right.  What *is* the correct solution?
          InputActor = new CheckboxActorClass(driver, new ByChained(selector, By.XPath("/../input")));
          break;
      }
  }
  // Do stuff with the InputActor, like InputActor.Select();
  </code></pre>