0

I am trying to use selenium to select a dining date(the website: https://inline.app/booking/-Lhcc8hgL2_MsPhLnZau:inline-live-2a466/-Lhcc9ECucQR5xXgsvL7?language=en)

The code I used is:

driver = webdriver.Chrome('/Users/agneschang/Downloads/chromedriver')
driver.get('https://inline.app/booking/-Lhcc8hgL2_MsPhLnZau:inline-live-2a466/-Lhcc9ECucQR5xXgsvL7?language=zh-tw');

driver.find_element_by_xpath('//*[@id="adult-picker"]/option[7]').click()
driver.find_element_by_id("date-picker").click()
driver.find_element_by_xpath("//*[@id="calendar-picker"]/div[2]/div[3]/div[1]/div[2]/button")

I copy the selected dining date's button but the last line in my code always show error. Is there any suggestion for me to click the date button?

ps. the html of the website is html:

Hsin
  • 5
  • 5
  • 1
    "but the last line in my code always show error" -- so would you have us guess what error you're getting? –  Dec 01 '20 at 07:46
  • Selenium should always be the last resort. Have you analysed the site to see if you cant replicate what it does using something simple like requests? – West Dec 01 '20 at 09:02
  • 1
    Maybe check your use of double and single quotes on the last line? Single quotes around 'calendar-picker'. – Jason Cook Dec 01 '20 at 12:11
  • @Agnes which dining date do you want to click on? – undetected Selenium Dec 01 '20 at 14:45
  • @JustinEzequiel sorry, i am trying to explain that i cannot make the webdriver click the dining date instead of saying the last line shows error :( – Hsin Dec 04 '20 at 11:04
  • @West Nah.... I didn't try any other way, selenium is the only way i know to write the auto booking system. But I will try requests on the site! Thanks a lot!! – Hsin Dec 04 '20 at 11:06
  • @JasonCook Yes, I tried it but It doesn't work – Hsin Dec 04 '20 at 11:07
  • @DebanjanB I want to click a date in the future 2 months. For example, today is December 4 and I want to book a date on Feb (any date should be fine as long as it's on Feb.) – Hsin Dec 04 '20 at 11:10

1 Answers1

0

Try to use WebDriverWait:

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

DELAY = 30

WebDriverWait(driver, DELAY).until(EC.presence_of_element_located((By.XPATH, "//*[@id='calendar-picker']/div[2]/div[3]/div[1]/div[2]/button"))).click()

The reason is that HTML elements are rendered by javascript in runtime and when selenium calls driver.find_element_by_xpath(), the element may not exist yet in the DOM tree. Usage of WebDriverWait allows selenium to wait till element needed will be added to DOM.

Alexandra Dudkina
  • 4,302
  • 3
  • 15
  • 27
  • OMG!!!!!!!! Thanks Alexandra!!!! It works!!!!!!!! Many thanks to the explaination to the runtime!!!! – Hsin Dec 04 '20 at 11:12