I am screen scraping with phantomJS in a python/selenium setup. Specifically, I need the first N elements that match one CSS selector.
The challenge is that there are more than double as much matching elements present within the website, so the call below takes too long (speed is a major requirement for me):
targets = WebDriverWait(driver, 1).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, css_selector)))
Is there any way to define a cutoff for found elements, like:
EC.presence_of_all_elements_located((By.CSS_SELECTOR, css_selector[0:N])))
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.
How to select the first n website elements matching one CSS selector in python selenium?