I'm new to Selenium and Java and I'm trying to select the nth element by CSS selector.
I want to get desired values from the code below and save them for further use. I have successfully used XPATH that looks like this
//*[@class='search-result__advert' and not(@id)][i] (i = nth element)
I'm trying to practice getting the values by CSS selector and I came up with this (doesn't work as expected)
.search-result__advert:not([id]):not([data-ad-show]):nth-child(i) (I've also tried :nth-of-type(i))
What I expect for i=3:
The third element that has no id attribute and no data-ad-show attribute is selected, the value would be Desired value 3.
Instead I get org.openqa.selenium.NoSuchElementException: Unable to locate element: .search-result__advert:not([id]):not([data-ad-show]):nth-child(3) It works fine for i=1, i=2, i=5.
<ul class="search-result list-unstyled">
<li class="search-result__advert">
<div class="grid grid--rev">
<p>Desired value 1</p>
</div>
</li>
<li class="search-result__advert">
<div class="grid grid--rev">
<p>Desired value 2</p>
</div>
</li>
<li class="search-result__advert" id="signUpWrapper">
<div id="signUpSavedSearch" class="grid">
<p>Advertisment 1</p>
</div>
</li>
<li class="search-result__advert hidden" data-ad-show="ad_list_middle">
<div class="ad" id="ad_list_middle">
<p>Advertisment 2</p>
</div>
</li>
<li class="search-result__advert">
<div class="grid grid--rev">
<p>Desired value 3</p>
</div>
</li>
<li class="search-result__advert">
<div class="grid grid--rev">
<p>etc. etc.</p>
</div>
</li>
</ul>