import time, os

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver import ActionChains
import requests
from bs4 import BeautifulSoup
import re
import schedule
from selenium.common.exceptions import TimeoutException
from datetime import datetime
import pyttsx3


trainnumber = "10"
tatkaltime = "11:03" #time for refresh
passengername = "jyoti"
passengerage = "19"
sex = "female"
day = "3" #enter the next day or day after. 2 for next day, 3 for day after.


def speak(text):
    # Initialize the pyttsx3 engine
    engine = pyttsx3.init()

    # Convert the text to speech
    engine.say(text)
    engine.runAndWait()


options = webdriver.ChromeOptions()
options.add_argument("--log-level=3")
options.add_argument("user-data-dir=C:\\Users\\user\\AppData\\Local\\Google\\Chrome Beta\\User Data\\")
options.add_argument("--start-maximized")  # use --start-maximized instead
options.binary_location = "C:\\Program Files\\Google\\Chrome Beta\\Application\\chrome.exe"

bot = webdriver.Chrome(executable_path="chromedriver.exe", chrome_options=options)
wait = WebDriverWait(bot, 60)
bot.get("https://www.irctc.co.in/nget/train-search")#open link

wait.until(EC.element_to_be_clickable((By.XPATH, '/html/body/app-root/app-home/div[1]/app-header/div[2]/div[2]/div[1]/a[1]')))#Wait for login button

Loginclick = bot.find_element(By.XPATH, '/html/body/app-root/app-home/div[1]/app-header/div[2]/div[2]/div[1]/a[1]')
bot.execute_script('arguments[0].click();', Loginclick)
bot.implicitly_wait(5)

popup = WebDriverWait(bot, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'ng-trigger')))
time.sleep(1)
Popup = bot.find_element(By.CLASS_NAME, 'ng-trigger')
bot.execute_script('arguments[0].click();', Popup)
print("mill gya")
for _ in range(5):
    bot.switch_to.active_element.send_keys(Keys.TAB)

print("Login text box is ready")
# Send the username
username = "Singh047"
bot.switch_to.active_element.send_keys(username)
# Press Tab to move to the next field
bot.switch_to.active_element.send_keys(Keys.TAB)
# Send the password
password = "Fw2IZxMl3"
bot.switch_to.active_element.send_keys(password)
# Press Tab to move to the next field
bot.switch_to.active_element.send_keys(Keys.TAB)
speak("Please find and fill captcha and click login")
print("done")

WebDriverWait(bot, 30).until(EC.presence_of_element_located((By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-main-page/div/div/div[1]/div[2]/div[1]/app-jp-input/div/form/div[2]/div[1]/div[1]/p-autocomplete/span/input')))#Wait for login done
time.sleep(1)
bot.find_element(By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-main-page/div/div/div[1]/div[2]/div[1]/app-jp-input/div/form/div[2]/div[1]/div[1]/p-autocomplete/span/input').send_keys("DD UPADHYAYA JN - DDU ")
time.sleep(1)
bot.switch_to.active_element.send_keys(Keys.TAB)
bot.switch_to.active_element.send_keys(Keys.TAB)
bot.switch_to.active_element.send_keys("NEW DELHI")
time.sleep(2)
bot.switch_to.active_element.send_keys(Keys.TAB)
time.sleep(1)
bot.switch_to.active_element.send_keys(Keys.TAB)
bot.switch_to.active_element.send_keys(Keys.TAB)
bot.switch_to.active_element.send_keys(Keys.TAB)
bot.switch_to.active_element.send_keys(Keys.RIGHT)#Select date
time.sleep(1)
bot.switch_to.active_element.send_keys(Keys.ENTER)
time.sleep(1)
bot.switch_to.active_element.send_keys(Keys.TAB)
time.sleep(1)
for _ in range(11):
    bot.switch_to.active_element.send_keys(Keys.DOWN)

time.sleep(1)
bot.switch_to.active_element.send_keys(Keys.TAB)
time.sleep(1)
bot.switch_to.active_element.send_keys(Keys.UP)
bot.switch_to.active_element.send_keys(Keys.UP)#Select tatkal

bot.switch_to.active_element.send_keys(Keys.TAB)
bot.switch_to.active_element.send_keys(Keys.TAB)
bot.switch_to.active_element.send_keys(Keys.TAB)
bot.switch_to.active_element.send_keys(Keys.TAB)
bot.switch_to.active_element.send_keys(Keys.TAB)
bot.switch_to.active_element.send_keys(Keys.ENTER)#Search Trains

WebDriverWait(bot, 10).until(EC.presence_of_element_located((By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-train-list/div[4]/div/div[5]/div[1]/div[1]')))#Wait for train list
time.sleep(1)


while True:
    # Check if the current time is 11:00
    current_time = datetime.now().strftime("%H:%M")
    if current_time == tatkaltime:
        # Find and click the refresh button
        Refresh = bot.find_element(By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-train-list/div[4]/div/div[5]/div['+trainnumber+']/div[1]/app-train-avl-enq/div[1]/div[5]/div[1]/table/tr/td[1]/div')
        bot.execute_script('arguments[0].click();', Refresh)
       
        # Exit the loop after clicking the refresh button
        break
   
    # Wait for some time before checking the time again
    time.sleep(2)  # Wait for 60 seconds before checking again

print("waiting for available ticket")
speak("waiting for available ticket")
WebDriverWait(bot, 120).until(EC.presence_of_element_located((By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-train-list/div[4]/div/div[5]/div['+trainnumber+']/div[1]/app-train-avl-enq/div[1]/div[7]/div[1]/div[3]/table/tr/td['+day+']/div')))#Wait for show available tickets
tickets = bot.find_element(By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-train-list/div[4]/div/div[5]/div['+trainnumber+']/div[1]/app-train-avl-enq/div[1]/div[7]/div[1]/div[3]/table/tr/td['+day+']/div')
bot.execute_script('arguments[0].click();', tickets)#Select ticket
time.sleep(1)
print("available ticket selected")
book = bot.find_element(By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-train-list/div[4]/div/div[5]/div['+trainnumber+']/div[1]/app-train-avl-enq/div[2]/div/span/span[1]/button')
bot.execute_script('arguments[0].click();', book)
time.sleep(2)
bot.switch_to.active_element.send_keys(Keys.ENTER)
WebDriverWait(bot, 120).until(EC.presence_of_element_located((By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-passenger-input/div[3]/div/div/div[1]/div[2]')))#Wait for passenger input details
time.sleep(1)

bot.find_element(By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-passenger-input/div[5]/form/div/div[1]/div[4]/p-panel/div/div[2]/div/div[1]/div[2]/div/app-passenger/div/div[1]/span/div[1]/p-autocomplete/span/input').send_keys(passengername)
time.sleep(0.5)
bot.switch_to.active_element.send_keys(Keys.TAB)
bot.switch_to.active_element.send_keys(passengerage)#Age

bot.switch_to.active_element.send_keys(Keys.TAB)

if sex == "female":
    for _ in range(2):
        bot.switch_to.active_element.send_keys(Keys.DOWN)
elif sex == "male":
    bot.switch_to.active_element.send_keys(Keys.DOWN)
else:
    # Handle other cases or provide a default behavior
    pass

#Add here book only if confirm birth is available

Confirmtik = bot.find_element(By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-passenger-input/div[5]/form/div/div[1]/div[10]/p-panel/div/div[2]/div/span/div[1]/div[2]/label')#Book only if confirm ticket avialable
bot.execute_script('arguments[0].click();', Confirmtik)
time.sleep(0.5)

UPI = bot.find_element(By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-passenger-input/div[5]/form/div/div[1]/div[12]/p-panel/div/div[2]/div/table/tr[2]/label/p-radiobutton/div/div[2]')
bot.execute_script('arguments[0].click();', UPI)
time.sleep(0.5)

Continuebutton = bot.find_element(By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-passenger-input/div[5]/form/div/div[1]/div[14]/div/button[2]')
bot.execute_script('arguments[0].click();', Continuebutton)
time.sleep(0.5)

speak("fill captcha")
WebDriverWait(bot, 120).until(EC.presence_of_element_located((By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-payment-options/div[4]/div[2]/div[1]/div[1]/app-payment/div[2]/button[2]')))#Wait for payment page

multipolepayment = bot.find_element(By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-payment-options/div[4]/div[2]/div[1]/div[1]/app-payment/div[1]/div/form/div[1]/span/div[2]')
bot.execute_script('arguments[0].click();', multipolepayment)
time.sleep(0.5)

phonepay = bot.find_element(By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-payment-options/div[4]/div[2]/div[1]/div[1]/app-payment/div[1]/div/form/div[2]/app-bank/div/div/table/tr/span[4]/td/div/div')
bot.execute_script('arguments[0].click();', phonepay)
time.sleep(0.5)

payandbook = bot.find_element(By.XPATH, '/html/body/app-root/app-home/div[3]/div/app-payment-options/div[4]/div[2]/div[1]/div[1]/app-payment/div[2]/button[2]')
bot.execute_script('arguments[0].click();', payandbook)
time.sleep(0.5)


# /html/body/app-root/app-home/div[3]/div/app-passenger-input/div[5]/form/div/div[1]/div[10]/p-panel/div/div[2]/div/span/div[1]/div[2]/label

time.sleep(600)

0 comments:

Post a Comment

 
Top