Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from Tkinter import *
- from selenium import webdriver
- from selenium.common.exceptions import TimeoutException
- from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
- from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
- import time
- from selenium.webdriver.common.keys import Keys # For CTRL clicking
- #import lxml.html # For parsing table data
- import re # For searching
- from selenium.webdriver.common.action_chains import ActionChains
- from selenium.common.exceptions import NoSuchElementException
- from selenium.common.exceptions import ElementNotVisibleException
- from ScrolledText import ScrolledText
- def login():
- driver.get("https://pm.officeally.com/emr/Login.aspx?ReturnUrl=%2femr%2fPatientCharts%2fManagePatients.aspx%3fTab%3dC&Tab=C")
- #window.update()
- inputUsername = driver.find_element_by_name("Login1$UserName")
- inputPassword = driver.find_element_by_name("Login1$Password$TextBox1")
- inputSubmit = driver.find_element_by_id("Login1_LoginButton")
- inputUsername.send_keys(username)
- inputPassword.send_keys(password)
- inputSubmit.click()
- #window.update()
- def closePopup():
- if len(driver.window_handles) > 1:
- driver.implicitly_wait(10)
- #print "Closing Popup"
- driver.switch_to_window(driver.window_handles[1])
- driver.find_element_by_id('Button3').click()
- driver.switch_to_window(driver.window_handles[0])
- def closeNews():
- try:
- driver.find_element_by_id('btnAcknowledge').click()
- print 'Closing News and Information message'
- except (ElementNotVisibleException, NoSuchElementException):
- return 1
- def closeAlerts():
- driver.execute_script('closeAlerts()')
- def closeTab():
- currentIndex = driver.window_handles.index(driver.current_window_handle)
- newIndex = driver.window_handles[currentIndex - 1]
- print "Closing tab"
- driver.close()
- driver.switch_to_window(newIndex)
- window.update()
- def nextTab():
- driver.implicitly_wait(10)
- currentIndex = driver.window_handles.index(driver.current_window_handle)
- driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.NUMPAD2) #MAC
- #driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + Keys.TAB) #WINDOWS
- print "Going to next tab"
- driver.implicitly_wait(10)
- driver.switch_to_window(driver.window_handles[currentIndex + 1])
- window.update()
- def searchFor(name):
- printOutput(("--------- " + name[0].title() + ' ' + name[len(name)-1].title() + " ---------"))
- driver.switch_to_window(driver.window_handles[0])
- driver.implicitly_wait(10)
- inputSearchPatients = driver.find_element_by_id("ctl00_phFolderContent_ucSearch_txtSearch")
- inputSearchPatients.clear()
- inputSearchPatients.send_keys(Keys.CONTROL, "a")
- try:
- inputSearchPatients.send_keys(name[len(name)-1].title())
- except NoSuchElementException:
- printOutput('ERROR')
- driver.implicitly_wait(10)
- driver.find_element_by_id("ctl00_phFolderContent_ucSearch_btnSearch").send_keys(Keys.RETURN) # Clicks Search
- driver.implicitly_wait(10)
- try:
- #grabs link from text and merges with chrome script
- linkstr = driver.find_element_by_partial_link_text(name[0].title()).get_attribute("href")
- theScript = str("window.open(\"" + linkstr + "\");")
- driver.execute_script(theScript)
- return 0
- except NoSuchElementException:
- printOutput('Can not find patient')
- global errorCount
- errorCount += 1
- return 1
- def checkLastNoteDate():
- driver.implicitly_wait(10)
- WebDriverWait(driver, 6).until(EC.visibility_of(driver.find_element_by_id('divSummary')))
- lastVisitDiv = driver.page_source.encode('utf-8')
- try:
- lastVisitDiv = driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_panelView_ctl02_grdSection"]/tbody/tr[2]/td[1]/p')
- lastNoteDate = lastVisitDiv.text[:10].split('/')
- printOutput(('Last encounter:' + ' ' + lastVisitDiv.text[:10]))
- if lastNoteDate == currentDate:
- return 0 # Already Exists
- return 1 # Previous Day Exists
- except (ValueError, NoSuchElementException):
- printOutput('No Previous Encounters')
- return 2
- def copyLastNote(date):
- printOutput('Copying last note')
- try:
- WebDriverWait(driver, 20).until(EC.visibility_of(driver.find_element_by_id('ctl00_phFolderContent_panelView_ctl02_grdSection')))
- closeAlerts()
- driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_panelView_ctl02_grdSection"]/tbody/tr[2]/td[6]/img').click()
- except TimeoutException:
- printOutput ("Loading took too much time!")
- fillEncounterDate(date)
- def fillEncounterDate(date):
- WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element_by_id('ctl00_phFolderContent_ucSOAPNote_EncounterDate_Month')))
- inputDateMonth = driver.find_element_by_id('ctl00_phFolderContent_ucSOAPNote_EncounterDate_Month').send_keys(Keys.BACKSPACE + Keys.BACKSPACE + date[0] + date[1] + date[2])
- driver.implicitly_wait(10)
- #time.sleep(1)
- submitNoteUpdate()
- def submitNoteUpdate():
- WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element_by_id('btnSOAPUpdate1')))
- driver.find_element_by_id('btnSOAPUpdate1').click()
- def openClinicalSummary():
- #time.sleep(2)
- driver.implicitly_wait(20)
- firstName = driver.find_element_by_id('ctl00_phFolderContent_myPatientHeader_lblFirstName').text
- lastName = driver.find_element_by_id('ctl00_phFolderContent_myPatientHeader_lblLastName').text
- driver.execute_script('javascript:document.getElementById(\"fraViewDocument\").contentWindow.ViewDoc(\'PatientVisitSummary\')')
- printOutput('Opening clinical summary')
- waitForSummary()
- #time.sleep(1)
- driver.execute_script('javascript:document.getElementById(\"fraViewDocument\").contentWindow.openMU()')
- driver.implicitly_wait(10)
- checkboxValue = driver.execute_script('return document.getElementById("MUChecklist").contentWindow.document.getElementById("dd2").value')
- if checkboxValue is 7 or 2:
- driver.implicitly_wait(20)
- driver.find_element_by_xpath('//*[@id="ucMUChecklist_btnUpdate"]').send_keys(Keys.RETURN)
- if (checkLastNoteDate() is 1):
- printOutput((firstName + ' ' + lastName + ' Successfully Added'))
- time.sleep(2)
- closeTab()
- else:
- print "oh shit"
- else:
- openClinicalSummary()
- def waitForMoreTabs(initial):
- if len(driver.window_handles) is not initial:
- return 1
- else:
- time.sleep(1)
- waitForMoreTabs(initial)
- def waitForSummary():
- driver.implicitly_wait(20)
- initialTabCount = len(driver.window_handles)
- currentIndex = driver.window_handles.index(driver.current_window_handle)
- waitForMoreTabs(initialTabCount)
- driver.switch_to_window(driver.window_handles[currentIndex + 1])
- WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element_by_xpath('//*[@id="form1"]/div[3]/div[2]')))
- closeTab()
- def createNewNote(date):
- printOutput('Creating new note')
- driver.implicitly_wait(20)
- ActionChains(driver).move_to_element(driver.find_element_by_xpath('//*[@id="mn-oamPatientCharts"]/div/div[2]/div[1]/a')).perform()
- driver.implicitly_wait(10)
- #time.sleep(1)
- WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element_by_xpath('//*[@id="mn-oamPatientCharts"]/div/div[2]/div[1]/a')))
- driver.find_element_by_xpath('//*[@id="mn-oamPatientCharts"]/div/div[2]/div[2]/table/tbody/tr/td[2]/ul/li/a').click()
- fillEncounterDate(date)
- def openLastDay():
- driver.implicitly_wait(10)
- printOutput('Opening last note')
- printOutput('(No new encounter created)')
- closeAlerts()
- driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_panelView_ctl02_grdSection"]/tbody/tr[2]/td[4]/img').click()
- def alreadyExists(name):
- printOutput(('Encounter for ' + name + ' already exists on ' + niceCurrentDate))
- f = open(patientsFile,"r")
- fileLines = f.readlines()
- f.close()
- f = open(patientsFile,"w")
- for line in fileLines:
- if line == name+"\n":
- f.write(name + " --Note Already Exists\n")
- else:
- f.write(line)
- f.close()
- def editLastNote(name):
- lastTab()
- try:
- closeAlerts()
- driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_panelView_ctl02_grdSection"]/tbody/tr[2]/td[5]/img').click()
- driver.implicitly_wait(10)
- #time.sleep(1)
- element = driver.find_element_by_id('imgHistory')
- driver.execute_script("arguments[0].scrollIntoView(true);", element)
- driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_ucSOAPNote_S_Allergies"]').send_keys(name)
- driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_ucSOAPNote_S_Allergies"]').send_keys(Keys.CONTROL + 'a')
- except NoSuchElementException:
- printOutput('Error editing last note')
- driver.implicitly_wait(10)
- firstTab()
- def lastTab():
- driver.implicitly_wait(10)
- driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + '9')
- driver.switch_to_window(driver.window_handles[len(driver.window_handles)-1])
- driver.implicitly_wait(10)
- window.update()
- def firstTab():
- driver.implicitly_wait(10)
- driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + '1')
- driver.switch_to_window(driver.window_handles[0])
- def addNewEncounters(patients):
- resetErrors()
- closeNews()
- #closePopup()
- driver.implicitly_wait(10)
- WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_myCustomGrid_myGrid_pager_center"]/table/tbody/tr/td[4]/input')))
- for currentPatient in patients:
- print currentPatient
- currentPatientList = currentPatient.split(" ")
- print currentPatientList
- # if patient has 3 names, merge the first two
- if (len(currentPatientList) > 2):
- print "merging names"
- currentPatientList[0] = str(currentPatientList[0]) + " " + str(currentPatientList[1])
- print str(currentPatientList)
- if (searchFor(currentPatientList) is not 1):
- nextTab();
- checkDate = checkLastNoteDate()
- if checkDate is 0: # Already exists
- openLastDay()
- openClinicalSummary()
- printOutput(('Encounter for ' + currentPatient.title() + ' already exists'))
- if checkDate is 1: # Previous note exists
- copyLastNote(currentDate)
- openClinicalSummary()
- if checkDate is 2: # No previous notes
- createNewNote(currentDate)
- openClinicalSummary()
- if errorCount is 0:
- printOutput('-------------------------------')
- printOutput('ALL PATIENTS SUCCESSFULLY ADDED')
- printOutput('-------------------------------')
- else:
- printOutput('--------------------------------------')
- printOutput(('ERRORS: ' + str(errorCount) + '. CHECK ABOVE FOR MORE INFO'))
- printOutput('--------------------------------------')
- def openDrugs(patients):
- resetErrors()
- closePopup()
- closeNews()
- driver.implicitly_wait(10)
- WebDriverWait(driver, 10).until(EC.visibility_of(driver.find_element_by_xpath('//*[@id="ctl00_phFolderContent_myCustomGrid_myGrid_pager_center"]/table/tbody/tr/td[4]/input')))
- for currentPatient in patients:
- currentPatientList = currentPatient.split(' ')
- searchFor(currentPatientList)
- lastTab()
- checkDate = checkLastNoteDate()
- editLastNote(currentPatient)
- printOutput('All patients opened')
- def resetErrors():
- global errorCount
- errorCount = 0
- def updateDate(date):
- lines = open('config.txt', 'r').readlines()
- lines[2] = "last date: " + date
- out = open('config.txt', 'w')
- out.writelines(lines)
- out.close()
- def getDate():
- dateInput = tkDateInput.get()
- updateDate(dateInput)
- printOutput(('Date entered: ' + dateInput))
- global currentDate
- currentDate = dateInput.split('/')
- global niceCurrentDate
- niceCurrentDate = currentDate[0] + "/" + currentDate[1] + "/" + currentDate[2]
- def printOutput(text):
- tkOutput.config(state=NORMAL)
- tkOutput.insert(END, str(text) + '\n')
- tkOutput.yview(END)
- tkOutput.config(state=DISABLED)
- window.update()
- def buttonOne():
- getDate()
- patients = tkPatientList.get(1.0, END).split('\n')
- print patients[:-1]
- addNewEncounters(patients[:-1])
- def buttonTwo():
- getDate()
- patients = tkPatientList.get(1.0, END).split('\n')
- print patients[:-1]
- openDrugs(patients[:-1])
- def initiateBrowser():
- global driver
- driver = webdriver.Chrome("Drivers\chromedriver.exe")
- #window.update()
- driver.set_window_size((window.winfo_screenwidth() - 655), (window.winfo_screenheight() - 100))
- login()
- #window.update()
- closePopup()
- #window.update()
- closeNews()
- def closeWindow():
- f = open('output.txt',"w")
- output = tkOutput.get(1.0, END)
- for line in output:
- f.write(line)
- f.close()
- window.quit()
- driver.quit()
- def pasteClipboard():
- text = window.selection_get(selection='CLIPBOARD')
- tkPatientList.insert('insert', text)
- with open('config.txt', 'r') as file:
- lines = [line.strip() for line in file]
- username = lines[0][10:]
- password = lines[1][10:]
- errorCount = 0
- window = Tk()
- window.title('Office Ally')
- tkPatientListLabel = Label(window, text="Patients: Output log: ")
- tkPatientListLabel.pack()
- tkPatientList = Text(window, width=24)
- tkPatientList.pack(side = LEFT)
- tkPatientList.focus_set()
- tkDateInput = Entry(window, width=10)
- tkOutput = ScrolledText(window, width=48, state = DISABLED)
- tkOutput.pack(side = RIGHT)
- b1 = Button(window, text="Add Encounters", width=16, command=buttonOne)
- b2 = Button(window, text="Add Drugs", width=16, command=buttonTwo)
- b3 = Button(window, text="Paste clipboard", width=16, command=pasteClipboard)
- b31 = Button(window, text="Open New Browser", width=16, command=initiateBrowser)
- b4 = Button(window, text="QUIT", fg="red", command=closeWindow)
- tkDateInput.pack(side = TOP)
- b1.pack(side = TOP)
- b2.pack(side = TOP)
- b3.pack(side = TOP)
- b31.pack(side = TOP)
- b4.pack(side = BOTTOM)
- text = ScrolledText(window)
- window.geometry("+%d+%d" % (window.winfo_screenwidth() - 645, window.winfo_screenheight() - 1075))
- tkDateInput.insert(0, time.strftime("%m") + "/" + time.strftime("%d") + "/" + time.strftime("%Y"))
- initiateBrowser()
- mainloop()
- closePopup()
- closeNews()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement