Skip to main content

Python - Kivy and Selenium - Kite application automation code

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.core.window import Window
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
import selenium.webdriver.support.ui as ui
from selenium.webdriver.support.ui import WebDriverWait
import selenium.webdriver.support.expected_conditions as EC
import os
import getpass
import time
import pymsgbox
Window.size = (300, 120)

#PcName = getpass.getuser()


class Kite(GridLayout):
def __init__(self, **kwargs):
super(Kite, self).__init__(**kwargs)
self.cols=3

self.btn_buy = Button(text="Buy")
self.btn_buy.bind(on_press=self.buy_button_click)
self.add_widget(self.btn_buy)

self.btn_sell = Button(text="Sell")
self.btn_sell.bind(on_press=self.buy_button_click)
self.add_widget(self.btn_sell)

self.txtOption = TextInput(id="txtoption",font_size=16,disabled=True)
self.add_widget(self.txtOption)


self.lab_current1 = Label(text="")
self.add_widget(self.lab_current1)

self.lab_current = Label(text="Current",font_size=16)
self.add_widget(self.lab_current)

self.txtCurrent = TextInput(font_size=16)
self.add_widget(self.txtCurrent)
#self.txtCurrent.text = PcName

self.lab_target1 = Label(text="")
self.add_widget(self.lab_target1)

self.lab_target = Label(text="Target",font_size=16)
self.add_widget(self.lab_target)


self.txtTarget = TextInput(font_size=16)
self.add_widget(self.txtTarget)

self.lab_target2 = Label(text="")
self.add_widget(self.lab_target2)

self.lab_target3 = Label(text="")
self.add_widget(self.lab_target3)


self.btnSubmit = Button(text="Submit")
self.btnSubmit.bind(on_press=self.Kite_Auto)
self.add_widget(self.btnSubmit)


def buy_button_click(self,instance):
print(instance.text)
self.txtOption.text = instance.text

def Kite_Auto(self,instance):
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--ignore-ssl-errors')
dir_path = os.path.dirname(os.path.realpath(__file__))
#Update Selenium chrome driver folder path here
chromedriver = dir_path + "/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chrome_options=options, executable_path= chromedriver)
driver.get("https://kite3.zerodha.com")
driver.maximize_window()
Crudoil = 0
target = float(self.txtTarget.text)
while Crudoil==0:
element = WebDriverWait(driver, 10000).until(EC.presence_of_element_located((By.XPATH, "//*[@id='app']/div[2]/div[1]/div/div[2]/div/div/div/div/span[2]/span[3]")))
Current_AMount = element.text
LCurrent= float(Current_AMount)
self.txtCurrent.text = Current_AMount
if LCurrent>=target and self.txtOption.text == "Buy":
replyV = pymsgbox.alert(text='Buy Target Achieved! - ' + str(LCurrent), title='', button='OK')
print(replyV)
break
if LCurrent<=target and self.txtOption.text == "Sell":
replyV = pymsgbox.alert(text='Sell Target Achieved! - ' + str(LCurrent), title='', button='OK')
print(replyV)
break
class Kite_Zerodha(App):
def build(self):
return Kite()

if __name__ == "__main__":
Kite_Zerodha().run()


Comments

Popular posts from this blog

Python - PyQt5 set Background image

import sys from PyQt5.QtCore import QSize from PyQt5.QtGui import QImage, QPalette, QBrush from PyQt5.QtWidgets import * class MainWindow ( QWidget ): def __init__ ( self ): QWidget. __init__ ( self ) self .setGeometry( 100 , 100 , 300 , 200 ) oImage = QImage( "test.jpg" ) sImage = oImage.scaled(QSize( 300 , 200 )) # resize Image to widgets size palette = QPalette() palette.setBrush( 10 , QBrush(sImage)) # 10 = Windowrole self .setPalette(palette) self .show() if __name__ == "__main__" : app = QApplication(sys.argv) oMainwindow = MainWindow() sys.exit(app.exec_())

pymsgbox - python package for message box

#This tutorial for to get a confirmation from the user by promting window #Python package "pymsgbox" needs to be install before run this below code #To install "pymsgbox" package go to command prompt and type in "PIP3 INSTALL pymsgbox" import pymsgbox #Displays a message box with OK and Cancel buttons. Number and text of buttons can be customized. Returns the text of the button clicked on. replyV = pymsgbox.confirm( text = 'Please login your application and click on on okay' , title = 'Login Confirmation' , buttons =[ 'OK' , 'Cancel' ]) print (replyV) #Displays a simple message box with text and a single OK button. Returns the text of the button clicked on. replyV = pymsgbox.alert( text = '' , title = '' , button = 'OK' ) print (replyV) #Displays a message box with text input, and OK & Cancel buttons. Returns the text entered, or None if Cancel was clicked. replyV1 = pyms