furas

Python - Selenium - gokasper.com (Stackoverflow)

Sep 15th, 2025 (edited)
190
0
Never
4
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. # date: 2025.09.15
  2.  
  3. # [How to adjust Selenium/Python code for data extraction using different websites - Stack Overflow](https://stackoverflow.com/questions/79765258/how-to-adjust-selenium-python-code-for-data-extraction-using-different-websites)
  4.  
  5. from selenium import webdriver
  6. from selenium.webdriver.support.ui import WebDriverWait
  7. from selenium.webdriver.support import expected_conditions as EC
  8. from selenium.webdriver.common.by import By
  9.  
  10. url = 'https://gokasper.com/'
  11. print('url:', url)
  12.  
  13. input_origin = 'Thunder Bay'
  14. input_destination = 'Winnipeg'
  15. departure_date = 'Sat, Sep 20'
  16.  
  17. driver = webdriver.Chrome()
  18. driver.maximize_window()
  19. driver.get(url)
  20.  
  21. waiter = WebDriverWait(driver, 20)
  22.  
  23. # --- origin ---
  24.  
  25. # open window with input field
  26. origin = waiter.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'p[aria-label="Select Origin Station"]')))
  27. origin.click()
  28.  
  29. # fill input field
  30. origin = waiter.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[placeholder="Type to search"]')))
  31. origin.clear()
  32. origin.send_keys(input_origin)
  33.  
  34. # select first result on list
  35. first_item_on_list = waiter.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.MuiStack-root.cursor-pointer')))
  36. first_item_on_list.click()
  37.  
  38. # --- destination ---
  39.  
  40. # open window with input field
  41. destination = waiter.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'span[aria-label="Destination"] p')))
  42. destination.click()
  43.  
  44. # fill input field
  45. destination = waiter.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[placeholder="Type to search"]')))
  46. destination.clear()
  47. destination.send_keys(input_destination)
  48.  
  49. # select first result on list
  50. first_item_on_list = waiter.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.MuiStack-root.cursor-pointer')))
  51. first_item_on_list.click()
  52.  
  53. # --- date ---
  54.  
  55. # not ready yet
  56.  
  57. #date = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#dateInput-from")))
  58. #date.click()
  59. #date.send_keys(departure_date)
  60. #WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-e2e='autocomplete-options-to']//span[contains(., " & departure_date & ")]"))).click()
  61.  
  62. # --- search ---
  63.  
  64. search_btn = driver.find_element(By.XPATH, '//button[span[contains(text(), "Search")]]')
  65. search_btn.click()
  66.  
  67. input("Press ENTER to close")
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment