title: How to select the first N website elements matching one CSS selector in python selenium?
body: <p>I am screen scraping with phantomJS in a python/selenium setup.
  Specifically, I need the first N elements that match one CSS selector.</p>
  
  <p>The challenge is that there are more than double as much matching elements present within the website, so the call below takes too long (<strong>speed is a major requirement for me</strong>):</p>
  
  <pre><code>targets =  WebDriverWait(driver, 1).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, css_selector)))
  </code></pre>
  
  <p>Is there any way to define a cutoff for found elements, like:</p>
  
  <pre><code>EC.presence_of_all_elements_located((By.CSS_SELECTOR, css_selector[0:N])))
  </code></pre>
  
  <p><em>My previous solution without CSS selectors was to copy all html in one call, convert it to text and then get the (text) elements of interest by text parsing, using a dict of words to keep/remove. This was more than 4x faster. However, this is not suitable/scalable since the dict needs to be maintained.</em></p>
  
  <p><strong>How to select the first n website elements matching one CSS selector in python selenium?</strong></p>