title: Make CSS Selector (first-child) work in Behat 3 with Sahi Driver
body: <p>I was just wondering if you could point out where the problem is here, if it's Behat, the CSS selector, or the Sahi Driver.</p>
  
  <p>We have just upgraded to Behat 3 and are using Sahi Driver (most recent open source version). We have found that any Behat test that uses the pseudo-element <code>first-child</code> now seems to break. </p>
  
  <p>Example: </p>
  
  <p>Step:</p>
  
  <pre><code>And I follow "#the-list tr:first-child .row-title"
  </code></pre>
  
  <p>(which contains an anchor element with the class row-title on it, see HTML)</p>
  
  <p>Error: </p>
  
  <blockquote>
    <p>Link with id|title|alt|text "#the-list tr:first-child .row-title" not found. (Behat\Mink\Exception\ElementNotFoundException)</p>
  </blockquote>
  
  <p>HTML:</p>
  
  <pre><code>&lt;tbody id="the-list"&gt;
      &lt;tr id="post-162382" class="post-162382 type-post status-publish format-standard hentry category-uncategorized alternate iedit author-other level-0"&gt;
          &lt;th class="check-column" scope="row"&gt;&lt;/th&gt;
          &lt;td class="post-title page-title column-title"&gt;
              &lt;strong&gt;
                  &lt;a class="row-title" title="Edit “Post Title”" href="https://domain.com"&gt;&lt;/a&gt;
              &lt;/strong&gt;
              &lt;div class="locked-info"&gt;&lt;/div&gt;
              &lt;div class="row-actions"&gt;&lt;/div&gt;
              &lt;div id="inline_162382" class="hidden"&gt;&lt;/div&gt;
          &lt;/td&gt;
  </code></pre>
  
  <p>CSSSelector.php (override we used with our old Behat, we left this file in)</p>
  
  <pre><code>/**
   * Makes all of the form field related steps allow CSS selectors.
   */
  class CSSSelectorContext extends MinkContext
  {
      /**
       * Finds an element via CSS or fails with a given error
       * 
       * @param $element string   A CSS selector string
       * @param $error Exception  An error to throw in case the element can not be found
       * @return object           An Element object
       */
      protected function findOrFail($element, $error){
          $element = $this-&gt;getSession()-&gt;getPage()-&gt;find('css', $element);
          if (!isset($element)){     
              throw $error;
          }
          return $element;
      }
  
      public function clickLink($link) {
          try {
              parent::clickLink($link);
              return;
          }catch (Exception $error){
              $link = $this-&gt;fixStepArgument($link);
              $link = $this-&gt;findOrFail($link, $error);
              $link-&gt;press();
          }
      }
  </code></pre>
  
  <p>When using the css selector in the Chrome console with jquery it selects the appropriate element. I went through the code and looked at the css -> xpath translations and then validated the xpath against the html that is produced on the site we are testing and it seems to be valid as well. The css selector also works with Goutte driver.</p>
  
  <p>Generated XPath:</p>
  
  <pre><code>    find(function(){
          var count = 0;
          while (_sahi._byXPath("("+"\/\/html\/descendant-or-self::*[@id = 'the-list']\/descendant-or-self::*\/*[name() = 'tr' and (position() = 1)]\/descendant-or-self::*\/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' row-title ')]"+")["+(count+1)+"]")) count++;
          return count;
      })()
  
  descendant-or-self::*[@id = 'the-list']/descendant-or-self::*/*[name() = 'tr' and (position() = 1)]/descendant-or-self::*/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' row-title ')]
  
  //html/descendant-or-self::*[@id = 'the-list']/descendant-or-self::*/*[name() = 'tr' and (position() = 1)]/descendant-or-self::*/*[@class and contains(concat(' ', normalize-space(@class), ' '), ' row-title ')]
  </code></pre>
  
  <p>When I change the CSS to:</p>
  
  <p>Step:</p>
  
  <pre><code>And I follow "#the-list tr .row-title"
  </code></pre>
  
  <p>It works because I believe it just picks the first tr from a list of them anyway, but we want to be able to use first-child of course.</p>
  
  <p>Thanks for your help!</p>