Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to select nth item from a CSS selector

[data-short-caption="itemName" i] .circle-base, 

this selector is identifying two items in the DOM, I need to select the second item, is there any way like we have in xpath to select the second item?

The HTML Structure is something like this:

<div class="selection" data-select-item="select-item">
    <div data-short-caption='itemName'>
      <div class=circle-base> </div>
    </div>
    
    <div data-short-caption='itemName'>
     <div class=circle-base> </div>
    </div>
</div>
like image 690
Sanat Avatar asked Nov 25 '25 10:11

Sanat


1 Answers

As per the HTML:

<div class="selection" data-select-item="select-item">
    <div data-short-caption='itemName'>
      <div class=circle-base> </div>
    </div>

    <div data-short-caption='itemName'>
     <div class=circle-base> </div>
    </div>
</div>

To identify only the second item you can use either of the following css-selectors based Locator Strategy:

  • Using nth-child():

    div.selection div:nth-child(2) > div.circle-base
    
  • Using nth-of-type():

    div.selection div:nth-of-type(2) > div.circle-base
    
like image 52
undetected Selenium Avatar answered Nov 27 '25 22:11

undetected Selenium