Looking to extract the first two divs of a specific class from a webpage using CSS selectors in Selenium is proving to be challenging. To illustrate this issue, let's use Stack Overflow as an example. When testing the selector in Chrome Dev Tools console, it functions correctly:
$('div.question-summary:eq(0)')
[<div class="question-summary narrow tagged-interesting" id="question-summary-27442616">…</div>]
$('div.question-summary:eq(1)')
[<div class="question-summary narrow tagged-interesting" id="question-summary-27442177">…</div>]
However, when attempting the same with Selenium WebDriver, an error occurs:
require 'selenium-webdriver'
driver = Selenium::WebDriver.for :chrome
driver.navigate.to('http://www.stackoverflow.com')
first_element = driver.find_element(:css, 'div.mainnav:eq(0)')
Selenium::WebDriver::Error::InvalidSelectorError: invalid selector: An invalid or illegal selector was specified
Is there an alternative way to define this selector? Or a method to make Selenium recognize and utilize it?