Skip to content

Instantly share code, notes, and snippets.

@jivendra
Created June 7, 2023 15:31
Show Gist options
  • Save jivendra/37775d292956273111b5d5a329d0bf26 to your computer and use it in GitHub Desktop.
Save jivendra/37775d292956273111b5d5a329d0bf26 to your computer and use it in GitHub Desktop.
Selenium Script that will automatically enter passenger details in the IRCTC form while booking tickets
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
from selenium.webdriver.chrome.options import Options
import os
# Specify the path where the chromedriver is stored
os.environ['PATH'] += r"C:/SeleniumDrivers"
options = Options()
options.add_argument("--disable-notifications")
driver = webdriver.Chrome(options=options)
options.add_argument("--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.5672.129 Safari/537.36")
# Visit the website
driver.get("https://www.irctc.co.in/nget/train-search")
# Wait for the passenger details page to load
target_url = "https://www.irctc.co.in/nget/booking/psgninput"
wait = WebDriverWait(driver,500)
wait.until(EC.url_to_be(target_url))
actions = ActionChains(driver)
actions.send_keys(Keys.TAB).perform()
actions.send_keys(Keys.TAB).perform()
actions.send_keys(Keys.TAB).perform()
actions.send_keys(Keys.TAB).perform()
element = driver.switch_to.active_element
element.send_keys("Name 1")
actions.send_keys(Keys.TAB).perform()
element = driver.switch_to.active_element
element.send_keys("42")
actions.send_keys(Keys.TAB).perform()
element = driver.switch_to.active_element
select = Select(element)
select.select_by_index(2)
actions.send_keys(Keys.TAB).perform()
actions.send_keys(Keys.TAB).perform()
element = driver.switch_to.active_element
select = Select(element)
select.select_by_index(2)
# Add more passengers
actions.send_keys(Keys.TAB).perform()
actions.send_keys(Keys.ENTER).perform()
actions.key_down(Keys.SHIFT).send_keys(Keys.TAB).key_up(Keys.SHIFT).perform()
actions.key_down(Keys.SHIFT).send_keys(Keys.TAB).key_up(Keys.SHIFT).perform()
actions.key_down(Keys.SHIFT).send_keys(Keys.TAB).key_up(Keys.SHIFT).perform()
actions.key_down(Keys.SHIFT).send_keys(Keys.TAB).key_up(Keys.SHIFT).perform()
actions.key_down(Keys.SHIFT).send_keys(Keys.TAB).key_up(Keys.SHIFT).perform()
# Second passenger details
element = driver.switch_to.active_element
element.send_keys("Name 2")
actions.send_keys(Keys.TAB).perform()
element = driver.switch_to.active_element
element.send_keys("17")
actions.send_keys(Keys.TAB).perform()
element = driver.switch_to.active_element
select = Select(element)
select.select_by_index(2)
actions.send_keys(Keys.TAB).perform()
actions.send_keys(Keys.TAB).perform()
element = driver.switch_to.active_element
select = Select(element)
select.select_by_index(1)
actions.send_keys(Keys.TAB).perform()
actions.send_keys(Keys.TAB).perform()
actions.send_keys(Keys.TAB).perform()
actions.send_keys(Keys.TAB).perform()
element = driver.switch_to.active_element
element.clear()
element.send_keys("9876543218")
time.sleep(50000)
@aahnik
Copy link

aahnik commented Jun 11, 2023

nice code @jivendra ...

this is really useful. what about making the code a little more generic..

  1. use a for loop to enter details of n passengers instead of repeating the code.
  2. read passenger details from csv
  3. read path of selenium driver from environment variable, instead of hardcoding them, so it can be used easily on a different machine

@OshekharO
Copy link

How about adding login also and clicking on book now. ( I mean to say fully automated irctc booking script)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment