Python selenium gives error while fetch nth element

Question:

<div class="booking-classes">
   <h3 style="text-align: center;">
      <p><a href="https://foreupsoftware.com/index.php/booking/20290#/teetimes" style="background-position:0px 0px;"><b><i><span style="font-size:16pt;line-height:115%;font-family:Arial, sans-serif;color:rgb(42,100,150);">Click
         here to return to the main book a tee time page</span></i></b></a>
      </p>
      <p><br></p>
      <p><span style="font-weight:700;color:rgb(51,51,51);font-family:'Arial Rounded MT Bold', sans-serif;font-size:16px;text-align:center;"><span style="font-size:14pt;">Restrooms are currently closed and only port-a-john facilities are available.</span></span><br></p>
   </h3>
   <div>
      <div class="row" style="margin-top: 1em;">
         <div class="col-md-4 col-xs-12 col-md-offset-4">
            <label for="schedule_select" class="hidden-xs">Facility</label>
            <select id="schedule_select" name="schedules" class="form-control schedules">
               <option value="3782" selected="">
                  Hominy Hill
               </option>
               <option value="3778">
                  Bel-Aire 18 Holes
               </option>
               <option value="3779">
                  Bel-Aire 9 Holes
               </option>
               <option value="3780">
                  Charleston Springs North Course
               </option>
               <option value="3781">
                  Charleston Springs South Course
               </option>
               <option value="3783">
                  Howell Park
               </option>
               <option value="4234">
                  Howell Park Back 9
               </option>
               <option value="3784">
                  Pine Brook
               </option>
               <option value="3785">
                  Shark River
               </option>
               <option value="3969">
                  Shark River Back 9
               </option>
            </select>
         </div>
      </div>
   </div>
   <button class="btn btn-primary col-md-4 col-xs-12 col-md-offset-4">Passing</button>
   <button class="btn btn-primary col-md-4 col-xs-12 col-md-offset-4">The</button>
   <button class="btn btn-primary col-md-4 col-xs-12 col-md-offset-4">Parcel</button>
</div>

In the above element if you see the div class booking-classes has 5 children h3, div, button, button, button I need to get the 5th element button inside it.

This is what I tried :

driver.find_element(By.CSS_SELECTOR, 'div.booking-classes:nth-child(4)'

But this gives me error

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"div.booking-classes:nth-child(5)"}

I tried to get 3rd 4th etc as well but none of them are workingm but when I do 0th element, entire booking-class element is returned

Asked By: GABDA

||

Answers:

div.booking-classes:nth-child(4)

This piece of code means: Find div of class booking-classes which is 4th element of it’s parent.
What are you looking for is

div.booking-classes > button.nth-child(x)

Edit:
To get nth child of any type use

div.booking-classes > *:nth-child(x)

Where x is index of the child

Answered By: Zenek

First off nth child does not start with 0, it starts with 1. Secondly what you are doing means get the 4th child div.booking-class which is wrong.

You can try

driver.find_element(By.CSS_SELECTOR, 'div.booking-classes > *:nth-child(5)')

This gives :

<button class="btn btn-primary col-md-4 col-xs-12 col-md-offset-4">Parcel</button>

Say you want to get 5th element which should be a button you can do

driver.find_element(By.CSS_SELECTOR, 'div.booking-classes > button:nth-child(5)')