How to automate apps using python (PyWinAuto Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys welcome back to the channel and in this video today i'm going to be showing you how to automate almost every single application in python using a specific python package called pi ronaldo so first things first um what's the first things first yeah install the python package by renato obviously i don't know why i forgot that so i'm going to be using command prompt for this sale command prompt open it and use pip install tyrion auto if you hit run it should for me it shows requirement already satisfied because i have installed prime another before for you guys it will be showing like um installing or downloading all those sorts of stuff so after that yeah i'm using spider ide of anaconda now if you want to use pycharm it's not an issue you want to use any other ide it's your wish not much of a problem now next what you gotta do is you have to the way you automate an application in python is in a few steps step one import the package step two start the application you wanna automate step three connect python to the back end of the application you wanna automate step four automate okay so let's begin step one import the package now how we do that is from pyrin auto does application import application i don't know why some of these packages have the function name and the package name the same some people get confused even pythons sometimes get confused and gives you errors i don't know why they do that okay so first things first we imported it second is we have to start the application now for that we will create a variable like suppose app equals application inside this type in backend equals uia now most of the applications like almost every application's backend is uia so this backend right here specifies the back end of the application you want to automate so just lay with uia in case in case badly i'm sorry sorry for that in case an error comes up because of the back end just type in win 32 this won't happen usually so just leave it uia and then you have to type in dots start and in this bracket is where you give the full path to your application so in case if you're automating um chrome for example you have to give the full path to chrome.exe it might be in program files it might be in app data you just have to find it using open file location in windows now in this tutorial i'm going to be showing you to automate notepad so the way you the reason i chose notepad is because notepad's path is inbuilt in pi when auto for tutorial purposes so i don't have to give the full path i just have to type in notepad.exe now if i hit run it should show up the notepad application good that was the second step now third step is connect to the application now one more thing you need to know is that you can automate right by avoiding the second step so instead of starting in automatically using python you can start it manually and connect to it also so how you do that i'll show you that after connecting to this application for now just create another variable like app equals um this whole thing till back end copy till here paste it and insert dot star type dot connect bracket open and then you put in the way you connect is using the title of the window so in this case notepad oh wait this is command prompt sorry ah come on notepad how you automate this window or any application is and i mean how you connect to it is using its title in this case it's on title space hyphen space notepad everything should be used if there's a there are spaces you have to put spaces if there are hyphens or any other characters everything should be intact so i'm going to type in untitled space hyphen space notepad now if i hit run you guys will see an error right here there are two elements that match the criteria now the reason this error comes up is that there are two windows with the same name in this case two notepads with the same title on title space hyphen notepad so python doesn't know which untitled spaced hyphen space nail pad to connect them so to avoid this just close both of them and restart the program simple hit run and it works perfectly now one more thing you need to know is timeout see you put in timeouts equals how much our seconds you want now the reason we use timeout is that notepad is a very simple application but if you're automating other complex applications like photoshop or i don't know chrome zoom these applications take a few seconds to start up so if there is even a one second delay this program will crash so what timeout basically does is timeout makes the program wait for a period of time until this application starts up that means suppose if i'm going to show you another way i'm gonna remove timeout i don't have notepad open if you guys can see this is also a way to automate applications by starting the application manually okay i have told you that before if i hit run now there will be an address showing that there's no element found called untitled hyphen notepad so that's because i didn't start the application now if i start the application manually and hit run it works without no errors like there are no errors right now it has successfully connected to the untitled space hyphen space notepad i didn't start it automatically i started manually now suppose i did the first adder you saw because i did not start the application it did not come open but if i put time out for let's just say 20 seconds and i hit run so as you guys can see the error does not come in quickly because there is no window open it is waiting for 20 seconds for that application to come up if the 20 seconds expires the program obviously crashes let's just wait think uh the program crashed after 20 seconds it waited for 20 seconds for the application to start it didn't start so it crashed now let's try it another way in this case i'm gonna just increase the timeout just in case and i'm gonna wait for a few seconds the error didn't come and now i'm gonna start notepad and if you guys can see here the program quit that means it has successfully connected to notepad.exe now there are there's one more way you can write this instead of writing it in two lines you can also write it in one line if you want to start and connect automatically if you are starting it manually just write it in one single just write the connect path if you are starting it you can write it in one line like this it makes no difference so if i hit run right now it'll start the application and connect to it with the timeout also so that's that that is step two and three together step four automation now for this the worst notepad i'm gonna take notepad if you guys can see there every single thing you see in this window like this title bar this text this buttons these menu items this whole text box every single thing can be automated and these elements have handles or wrapper objects or control identifiers if you may so you have to find these lines these handles will be a line of code which you use to automate these stuff so suppose if you for firsts we will automate this giant text box we'll be typing stuff in so to find that handle of the text box you have to print the control identifiers now how you do that is you type an app which is the application you started and connected dot now this is one part where it's tricky you see the title here is on title space iphone space notepad now the way you write it without quotes is different so just uh for now i'm gonna type in untitled notepad now what this is is nothing but the same title in a different format so how we do this you have to remove the spaces you have to remove all of the characters like hyphen underscore dot everything and i'll join the to join all the words together and capitalize the first letters of both the words all the words so let's take another example like um new tab hyphen google chrome this is what you see at the first when you open google chrome i'm going to show you that if i open google chrome now if you guys can see here it's new tab hyphen google pro okay now got you google it was my phone my artificial intelligence opened up when i said google it's good it responds that's nice okay so back to the track um so as i said a new space tab space hyphen space google space chrome is a title so the way you write it is new tab google chrome so step one remove spaces remove all the other characters step two merge the characters together step three capitalize the first letters okay first letters of every single word so then you get new tab google chrome and capitals the same thing i did it with untitled notepad i remove the spaces oh come on sorry for that uh so where was i yeah so what we did here in untitled notepad is we remove the spaces we remove the other characters we merge the words together and then we capitalize the first letters but in this case the first letters are all really capitalized so it doesn't make any difference now we have to print the control identify so for that you just type in dot print underscore underscore identifiers wrap it up and back up close that two dots wait what happened to our untitled notepad untitled notepad if you hit one you will see a giant tree right here you see this this whole tree this tree consists of the control identifiers for every single element in this whole window so first we have to find the control identifier or the handle for this giant text box so if you scroll down and read the names of each and everything you will see text editor which is obviously the text editor or the giant text box where you type in stuff let's the and the handle of that text editor is this line of code this child window and the whole thing let's look another example let's just scroll down these all are useless we don't use that if you want you can um let's see yeah right here there's minimize maximize and close which are the minimize maximize and close buttons right here if you go a bit more down you guys can see file edit format view and help which is the menu items right here file edit format view and help now let's start by automating the text box where you do it is you comment this line we don't need it anymore because we already used it once we do it is you type in app dot untitled notepad the usual what you do is uh app is the application no control notepad is the heading and then you take the handle which is in this case text editor this whole line copy this put a dot paste this and type in dot wrapper underscore objects okay so the method is first you type in the applications variable which you started or connected then you put in untitled.nope and i will notepad the title of the window in a specific format then you put the handle of the element you want to automate then you put dot wrapper object and all before that you also have to put this in a variable like let's say text editor okay exterior equals this whole handle so this text editor consists of the handle to automate the text box so to send us so to type stuff in a text box to interact with it what you do is you type in text editor dot type underscore keys racket open bracket flows now inside this you open quotes like double quotes if you want single quotes no issue and type in what you want to type so in this case so subscribe to the channel it's free and i mean it it's true subscribe it's right there please help me okay so now if you hit run it starts the application it connects to it and it types and subscribe to the channel it's free i mean it but if you guys notice there are no spaces in between the words so how to fix that good question the way you fix that is you type in width underscore spaces equals true now with underscore spaces is default false that's why there are no spaces in the text which it wrote but if you put it with underscore spaces it puts spaces simple duh hit run and as you guys can see it started it connected it and it's typed it now what we're going to automate next is these buttons right here this file let's just take file for now let's close this don't save let's just check for the file control item which is right here this one is the files control item it's written here file file and control item menu item so copy this and the usual protocol first variable let's say file menu equals [Music] app dot untitled notepad then dot um control me child window dot wrapper underscore object okay now what you gotta do is you have file menu app until notepad window yeah that's it and now how do you interact with it is now for text box you use type keys to type your keys in it but for file menu what you use is or menu item the text editor is called edit control type is edit as you guys can see here menu items are called menu items yeah so that's that and for that you type in click on the school input right open parenthesis and close parenthesis now if you run the program again it starts connects types and clicks fully automated that's how you do it now another thing is wait suppose you want to automate something inside this in the inside file so new new window open save as all those stuff but you don't see that in here in the tree in that tree you didn't see open or save as or new window none of those came in because when you printed the control identifiers it only printed the controller device off this window i mean the window without the text but in case if you want the control identifies of this one you have to print control identifiers with this thing open or if if there is any other new dialog box you have to print control id device with that specific dialog box open so in this case i have opened the dialog file menu in this line and in the next line i'm going to print control identifies again so it will print a new tree with new elements as you guys can see it clicked on the button and it started printing a new control a new tree this one is the latest and new tree right here so let's just increase the size of this let's go to the top and start again so if you look down yeah it's right here see this is the file menu item right here and this is a drop down list that came up so there's new new window open save save as page setup print and exit everything inside this menu tab new window open save as blah blah blah all that stuff so now what i'm going to use is new window for this test so i'm going to as usual copy the handle i'm going to be fast forwarding a bit you know the process or never mind i'm not going to fast forward so create a new variable like um let's say new window equals app dot untitled notepad i'm gonna just copy this i don't know why i'm writing okay we control copy the control identifier and then we type in wrap underscore object we close the thingy and then we type in new window dot click input as usual in the top because it's a menu item you guys can see it right here for menu items you use click input dot click input parentheses open and close now if you you have to close this otherwise you will get the error of two different windows open so click down save and hit subscribe to the channel and new window so as you guys can see it clicked on the new window right here and it this is the old window and this is the new window so that's that now next what we're going to do is we are going to close this window okay so the way you do it let's just comment this part and try it again now there's a catch for closing the window suppose if there is text in the notepad and if you close it it does not close as it is it brings in another dialog box so as usual the same protocol you did it when you wanted to automate something inside the file menu item you have to print control identifiers after hitting the close button so that you can get the control identifiers of this box okay so what you got to do is you got to find the control identify for close first which was here right here see how convenient control c we'll type in close equals app dot untitled notes pad dots the whole thingy and wrap underscore object bracket close okay so then now as close is a button as you guys can see here it you can work with both dot click or with dot click underscore input in this case first let's try with dot click if it doesn't work we're gonna go without click input okay so we're gonna hit run it doesn't work it only clicked till file it didn't click the close button okay so what you're gonna do is you do click underscore input for some buttons click works and for some buttons click doesn't work okay but click input works for all buttons let's hit run and as you guys can see it clicked on close and here is where we print the control identifiers so we copy this whole line again we paste it right here and we hit one and there is the new tray the new tree full of controlled files let's just expand the console for a bit pull this up so that it's easy for us to see go to the top as usual and here it is do you want to save to save changes to untitled see the same text right here and if you go down you guys can see save down save and cancel see it's right there say you've done save and cancel so i'm going to use don't save because i don't want to save i'm lazy to save so let's copy this whole one ah come on copy this whole line and then we remove control identifies once again because we don't want to use it again then we create a new variable like down save equals copy till here ctrl c ctrl v dot the handle then dot wrapper underscore object around this close and after this we will as this is a button again we will try click so don't say dot click and if we hit run and there you go we close the window so that's how you automate buttons menu items and text boxes and all the stuff so i think that's all for this video now if you guys want to see me automate zoom using this method put it down in the comments and i don't know vegas can be chooses so 10 likes on this video and i'll put a video on how to automate zoom with this method you know what let's just make it 20. 20 likes on this video and i'll show you how to automate zoom are your zoom classes so you don't have to wake up early with this method well i guess that is it for today and i guess i'll see you guys in the next video bye-bye oh yeah and please do subscribe and uh the normal stuff subscribe like you
Info
Channel: RandomGuy
Views: 17,151
Rating: 4.7945948 out of 5
Keywords: pywinauto, python, pywinauto tutorial, automation, how to automate apps using python
Id: R4E4IOIC63s
Channel Id: undefined
Length: 26min 26sec (1586 seconds)
Published: Fri Jan 29 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.