Solution 1 :

Try this code:

golfers_text = driver.find_elements(By.CSS_SELECTOR, ".panel.panel-default.teetime .panel-body")

i = 0
for golfer_text in golfers_text:
    value = golfer_text.text
    print(value)
    if "Up to 4 golfers" in value:
        driver.find_element(By.XPATH, "(//*[@class='panel panel-default teetime']//div[@class='panel-body']/button)[" + str(i + 1) + "]").click()
        break
    print("------")
    i += 1

Solution 2 :

In the documentation I found this:

driver.find_elements(By.XPATH, '//button')

https://selenium-python.readthedocs.io/locating-elements.html

a quick example I wrote up is:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By

from webdriver_manager.chrome import ChromeDriverManager

driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get("https://www.theregister.com/")
elements = driver.find_elements(By.XPATH, '//article')

first = elements[0]

Problem :

I would like to know how to loop through things in Selenium. Basically, I just want to make a for-loop to find all the “panel titles” that have earlier than a certain time and then an if statement that says if it has 4 golfers then I will click the book button.

I’m just not sure how you iterate through the xpaths to these things in HTML. All I have been able to do so far is just click on the correct Day with my code below, however I can’t figure out

    ### Gets Edge driver and doesnt need extension update
    driver = webdriver.Edge(service=EdgeService(EdgeChromiumDriverManager().install()))
    driver.get(
        'https://teewire.net/granada/'
    )
    driver.maximize_window()
    pause(2)
    
    data_moment = "2022-10-30"
    driver.find_element(By.XPATH,f"//*[@id='gz-time-slot-calendar']//a[@data-moment='{data_moment}']").click()
    pause(5)
    
    data_moment = "2022-10-30"
driver.find_element(By.XPATH,f"//*[@id='gz-time-slot-calendar']//a[@data-moment='{data_moment}']").click()
pause(5)

    a = driver.find_elements(By.XPATH,"//*[@id='time-slots-container-id']//a[@class='{'panel-heading'}']")
    pause(2)
    for i in a:
        print(i.text)

Here’s an attached file. HTML

Comments

Comment posted by Monofuse

I’ve never used selenium in I’m assuming javascript or java, but the C# has a plural find. So instead of “find_element” its “find_elements”, that should return an array.

Comment posted by sammytay10r

Sorry, I forgot to include it is python. I did try find_element(s)

Comment posted by AbiSaran

Which data you want to retrieve, Panel title means the time or View Rates or the count of golfers?

Comment posted by sammytay10r

@AbiSaran I see, I didnt realize it carried all that information. I would just like to know the times really, and then eventually just make an if statement that says if 4 golfers: click the button containing that time slot.

Comment posted by AbiSaran

Posted an answer, check it.

Comment posted by sammytay10r

This is a great answer, Thank you so much this is super helpful. I have not seen that way of making a path with css selector. It seems like the most simplistic way of doing this.

Comment posted by AbiSaran

Glad it works for you.

Comment posted by sammytay10r

This was helpful, you can get the elements with find_elements using the xpath

By