Home > Blockchain >  Selenium - Iterate Through Grid Elements?
Selenium - Iterate Through Grid Elements?

Time:09-21

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()

CodePudding user response:

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.

CodePudding user response:

This is how you can click on the time-slot buttons - note the window needs to be sufficiently large. In case you are trying to do this headless, you want to make it bigger than FHD size.

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

chrome_options = Options()
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('disable-notifications')

chrome_options.add_argument("window-size=1280,1080")

webdriver_service = Service("chromedriver/chromedriver") ## path to where you saved chromedriver binary
browser = webdriver.Chrome(service=webdriver_service, options=chrome_options)
wait = WebDriverWait(browser, 20)
actions = ActionChains(browser)


url = 'https://resy.com/cities/ny/lartusi-ny?date=2022-09-20&seats=2'
browser.get(url)

hour_slot = wait.until(EC.element_to_be_clickable((By.XPATH, '//div[@]//div[@ and text() = "10:00PM"]/ancestor::button')))

hour_slot.click()
print('clicked!')

Selenium docs can be found at https://www.selenium.dev/documentation/

CodePudding user response:

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
  • Related