picklerick
picklerick

Reputation: 15

Selenium - Iterate Through Grid Elements?

enter image description hereWorking on a project to make reservations and I'm very rusty. I am able to navigate dynamically to the page for a reservation 2 weeks out, but I am unable to locate and click on the time slots.

My final line throws me an error, but my ultimate goal is to develop a code block that will iterate through the available time with some ranking system. For example, I set a ranked order of 8pm, 7:45pm, 7:30pm, 7:15pm, 8:15pm, etc. These time slots go fast, so I'll have to be able to handle the possibility of the reservation being gone or even taken while completing the checkout process.

I know this is a lot, so any help or guidance is appreciated!

from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
import datetime

ResDate = datetime.date.fromordinal(datetime.date.today().toordinal()+14).strftime("%Y-%m-%d")
print(ResDate)

URL = "https://resy.com/cities/ny/lartusi-ny?date={}&seats=2".format(ResDate)

timeout = 30
driver = webdriver.Chrome()
driver.get(URL)

TimeBlock = WebDriverWait(driver, timeout).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, '10:00')))
TimeBlock.click()

Upvotes: 1

Views: 620

Answers (2)

Arundeep Chohan
Arundeep Chohan

Reputation: 9969

wait = WebDriverWait(driver, 3)
ranking_list=['8:00PM','7:45PM','10:00PM']
for rank in ranking_list:
    try: 
        wait.until(EC.element_to_be_clickable((By.XPATH,f"//div[@class='ReservationButton__time' and text()='{rank}']"))).click()
        wait.until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@aria-label='Book with Resy']")))
        wait.until(EC.element_to_be_clickable((By.XPATH,"//button[./span[.='Reserve Now']]"))).click()  
        break
    except:
        print('No availability: ',rank)

Imports:

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC

Basically access each element of your ranking_list and then proceed to click on the reservation with that text. You can exit the loop if you can click on the reservation with break optional.

Upvotes: 1

AbiSaran
AbiSaran

Reputation: 2678

I didn't get your question about the ranking system. But for button clicking issue, try this code:

   time_to_book = "10:15PM"
   time_in_a_day = driver.find_elements(By.XPATH,"//*[@class='ReservationButton__time']")
   # print(len(time_in_a_day))

   time_text = []
   for i in range(len(time_in_a_day)):
       time_text.append(time_in_a_day[i].text)

   for i in range(len(time_text)):
       if time_text[i] == time_to_book:
           element = driver.find_element(By.XPATH,"(//*[@class='ReservationButton__time'])[" + str(i + 1) + "]//parent::button")
           driver.execute_script("arguments[0].click();", element)
           break

Upvotes: 0

Related Questions