Automate your job with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
learning how to automate stuff in Python is a great skill to have it's something that I started doing when I was learning and something I still do today even though I have access to Modern apis Etc so if your job revolves you around using a browser-based system then we can absolutely automate good parts of it using playwright so playwright is browser Automation in Python and we're going to be leaning heavily on its code gen feature which is going to do the majority of the heavy lifting for us I'll show you what I mean so I've got vs code open here and I have a new folder so what you want to do is you need to install playwright like this pip install playwright I've obviously already got it installed the next thing you want to do is do playwright install and type chromium so we only want to install the Chrome browser because that's the one we're going to be using if you install the other ones they'll just be there for until you delete them no need now we want to do playwright code gen like this this is going to open up a couple of extra windows and I'm going to move these over to my other screen just so we can go through them so what they do is the one on the left is the current code and the one on the right is the browser so as we make changes to the browser page it's going to record the appropriate action in the actual code itself so I'm going to paste in a URL here and we'll see we now have this page dot go to the next thing you want to do is fill in any login details that you need now this is already filled in here so I'm just going to just leave that at the moment and hit login but ideally you want to click on that so what we're going to do is we're going to I'll just move this over a little bit so it's a bit easier to see what we're going to do is we're going to create a new discount in this Ecom system so I'm going to click promotions then discount notice how the things are changing on the left hand side now I'm going to go to add new there's another click but dot click so let's give it a name by clicking in the name box and we'll just call this 50 off discount assigned to Total order click the use percentage and then we're going to put 50 in here now we want to add a coupon code so let's just do 50 off as the coupon code and we're going to do a start date and an end date now I'm going to do this by clicking on this to see if this works but you could of course type into the box here the date and times that you need now I'm going to hit save and done that's it okay so now let's go ahead and stop recording let's copy all of this and paste it into our code here and then close out these two browsers here so now we have the basis of what we need to do to basically automate that large part of our job we've gone ahead and we've logged in we can see that happening here I only clicked on the email and password remember because that was already in there so if you need to add some text in here click on it and then type the appropriate text in and you can see we're clicking on all these buttons now we are going to need to tidy this up a bit because it will probably fail at some point so what we're going to do is we're going to click run and then we'll see where it fails see where it stops and make the appropriate changes okay so let's run that and see what happens okay so it's opening up the page logging in fine go to Promotions Okay so we've hit our first sticking point so let's close that out and it failed under the promotions so where it was going to Promotions so let's add in page dot weight for load State this is the favorite one they like to use and it is um Network idle like this so let's try that again okay so that worked creating a new one this has worked and as I expected so it's failed on the uh the date input thing here that has on work so we need another solution for that so let's quickly let's have a look at the selector for this um okay you can see it failed on this parent settings div okay so let's just try commenting this out for the moment and seeing if we get anywhere okay that seemed to work so what we're going to do now is I'm going to import in time and at the best before we close everything off I'm going to put in time sleep and we'll say five seconds for now but what you can do is you can actually change these bits of information so I'm going to change this to 25 off and I'm going to change the name to 25 off as well so let's run it again and this time the sleep at the end is just going to give us a chance to see that it actually did it otherwise we can just log in otherwise again there we go before it closes we can see we have our 25 off I actually left it as 50 off so that's uh that's probably something you'd want to avoid doing but now you can see how powerful this is that was only a few minutes using its code generator to whip through and create a script that actually creates something on the system now you have to be careful when you do this you have to make sure that what you're doing is repeatable because the browser is just running through the commands that you're giving it so if things change it will fail but if a completely repeatable tasks where the same thing happens over and over again this is a great way to get it done if you've enjoyed this video I think you're going to enjoy this one right here which is more automation with python but this time for grabbing data
Info
Channel: John Watson Rooney
Views: 376,758
Rating: undefined out of 5
Keywords: python automation, python palywright, jobn watson rooney, python playwright, playwright automation, python automation tutorial, python automation projects, python automation ideas
Id: qYSWWGz9Z6s
Channel Id: undefined
Length: 6min 7sec (367 seconds)
Published: Fri Feb 10 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.