Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # date: 2025.09.15
- # [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)
- from selenium import webdriver
- from selenium.webdriver.support.ui import WebDriverWait
- from selenium.webdriver.support import expected_conditions as EC
- from selenium.webdriver.common.by import By
- url = 'https://gokasper.com/'
- print('url:', url)
- input_origin = 'Thunder Bay'
- input_destination = 'Winnipeg'
- departure_date = 'Sat, Sep 20'
- driver = webdriver.Chrome()
- driver.maximize_window()
- driver.get(url)
- waiter = WebDriverWait(driver, 20)
- # --- origin ---
- # open window with input field
- origin = waiter.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'p[aria-label="Select Origin Station"]')))
- origin.click()
- # fill input field
- origin = waiter.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[placeholder="Type to search"]')))
- origin.clear()
- origin.send_keys(input_origin)
- # select first result on list
- first_item_on_list = waiter.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.MuiStack-root.cursor-pointer')))
- first_item_on_list.click()
- # --- destination ---
- # open window with input field
- destination = waiter.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'span[aria-label="Destination"] p')))
- destination.click()
- # fill input field
- destination = waiter.until(EC.element_to_be_clickable((By.CSS_SELECTOR, 'input[placeholder="Type to search"]')))
- destination.clear()
- destination.send_keys(input_destination)
- # select first result on list
- first_item_on_list = waiter.until(EC.element_to_be_clickable((By.CSS_SELECTOR, '.MuiStack-root.cursor-pointer')))
- first_item_on_list.click()
- # --- date ---
- # not ready yet
- #date = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#dateInput-from")))
- #date.click()
- #date.send_keys(departure_date)
- #WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@data-e2e='autocomplete-options-to']//span[contains(., " & departure_date & ")]"))).click()
- # --- search ---
- search_btn = driver.find_element(By.XPATH, '//button[span[contains(text(), "Search")]]')
- search_btn.click()
- input("Press ENTER to close")
Advertisement