1

I am using python + selenium to automate common behavior through a website. I have now arrived at a point where there is a div <div class="teetime-grid" data-baan="1"> with a number of <div class="btn btn-success teetime has-bookings> inside <a href> tags. Please find a snippet of the HTML page below to clarify.

<div class="teetime-grid" data-baan="1">
    <a href="#">

        <div class="btn btn-success teetime has-bookings " data-time="1645165200"
             title="Er zijn nog 3 beschikbare plaatsen op deze starttijd">
            <span class="time">07:20</span>
            <div class="icons">
                <span class="icon bezet geslacht-m">&nbsp;</span>
                <span class="icon vrij">&nbsp;</span>
                <span class="icon vrij">&nbsp;</span>
                <span class="icon vrij">&nbsp;</span>

            </div>
        </div>
    </a>

    <a href="#">

        <div class="btn btn-success teetime has-bookings " data-time="1645165800"
             title="Er zijn nog 1 beschikbare plaatsen op deze starttijd">
            <span class="time">07:30</span>
            <div class="icons">
                <span class="icon bezet geslacht-m">&nbsp;</span>
                <span class="icon bezet geslacht-m">&nbsp;</span>
                <span class="icon bezet geslacht-m">&nbsp;</span>
                <span class="icon vrij">&nbsp;</span>

            </div>
        </div>
    </a>
</div>

I need to access specific <div class="btn btn-success teetime has-bookings> inside the grid. In Selenium, I know I can do this by using:

driver = webdriver.Chrome()
driver.find_element_by_xpath("//a[2]/div")

To select the second tag in my above example, which in this case has a corresponding time of 07:30 associated with it (it's defined in the <span class="time">07:30</span>).

Is there also a way to access these elements by the contents of the <span class="time"> instead of specifying the number in the list of <a href> tags? Because now I still have to know which a[?] number corresponds to a certain time. If I have to look this up manually every time time there is no point in automating this browser action to begin with.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Psychotechnopath
  • 2,471
  • 5
  • 26
  • 47

2 Answers2

1

This will find the div, which contains a specific span child 1 level down.

//span[@class='time'][text()='07:30']/..

And if you'll add one more /.., you'll find the a (the div parent).

Max Daroshchanka
  • 2,698
  • 2
  • 10
  • 14
1

To access the specific <div class="btn btn-success teetime has-bookings> corresponding to the different time you can the following locator strategies:

  • Selecting 07:20:

    element = driver.find_element(By.XPATH, "//a[./span[text()='07:20']]")
    
  • Selecting 07:30:

    element = driver.find_element(By.XPATH, "//a[./span[text()='07:30']]")
    
  • Note : You have to add the following imports :

    from selenium.webdriver.common.by import By
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352