Advanced Coding – Write Bots with Logging Feature
Hi everyone. Good evening. So this is a little followup video for the stateful programming, and I will release some additional videos over the next couple of days. Though, the first one I would like to give you an idea of how to implement some logging into your bot, with just a very simple example. So, in the last video I explained why stateful programming and state of airbots are important. And if you did not watch that video, please do so first so that you know what we are talking about here. So in this video I will show you an idea how to implement logging for the click command. So we have the launcher, and we are on the Amazon page, and I wrote my own defined command called My Click. Let’s take a quick look into that. So it’s a command, it has the name My Click so that it shows up in my commands as My Click.
I specified two parameters, a variable Xpath And a variable lock data. So the first thing what I do is in an if command, I do in comparison, and I check the return element count on the Xpath expression. So what this will do, it will check the site and validate that, or if we get less or more than one result. So you should write Xpath expressions that only return one result, because otherwise, you’re not never sure if you interact with the right elements. Sometimes, even when you have multiple hits, that Xpath expression finds multiple elements, and by luck the button you want to click is the first one, then your Xpath expression might work even too. It’s not unique. I highly recommend that you spend some time writing proper Xpath expressions who are unique, so they only return one.
So with that comparison, we will find out if it’s more, or if it’s zero. You can make that My Click define even more complex. And you add more checks. You could check, does it return more than one? So, that will also tell you if this worked before. And then at some point when you run your script, it will return more than one result that will tell you that something has changed from the side. You could also add another if/then check and instead of using does not equal here, you could use more than, or higher than one. And add an additional if check for equals zero. So that you can check also for the scenario if that element is not there anymore. Because here it just only compares if it’s not equal one.
So it could mean more than one, or less than one. So we do not distinguish in this example, but you can make that as complex as you want to. So, if it’s not equal one, then it will run append to log.txt. So it will, if it’s not there, create a log.txt, And at the beginning I will delete that lock file. So that when the bots or that process starts, we create a fresh lock file. You can also use variables here, like the date or the time. So that you have multiple lock files, or you could increment a variable. The first time you run it, it’s lock one, then lock two, lock three and so on. Whatever you want. And then I write a text error. I use the date variable, the date function to return to date and time.
Then I use Xpath, I add Xpath expression, so that when I look at the lock file, I see what expression was, or had an error. Then I add the lock data. Lock data is a variable that I can define. So I will use some specific text that will be then added to that lock file. I will specify the error element count not one. And I add new line, so that I can run it multiple times. So then, else. So that means else if the condition is not true, which means it is one, then it will click. It will execute the ExBrowser click command of the Xpath expression and it will append to the file. Also success Xpath, and so on. You could add date here as well.
Whatever you want to see in that lock file. So now then instead of in your code using the click command directly, you always use the My Click command. And you add the Xpath expression and you specify the lock data. And that way, the defined will always check if the Xpath expression has only one match, and also adds to the lock file. So when we run that now, and that’s another on the stop command here. It’s not launching the stuff again. So the process still already open. So we run that. Well first let’s take a look at the lock file. It’s empty now. So we run that, execute the first command, try that. And then the second one and that’s it. So now we go to the lock file. So the lock has modified reloaded.
So now you see, the first one was an error, because I specified and just slash A which targets every A elements, or multiple on that side, not just one. Which means it’s not unique element count not zero. So it adds that to the lock file of the error of the data. It specifies the lock data. So the text we specify, it first click main button. And then we also have a successful one. So that way, if you have multiple click commands, and you can do that for other commands as well. Write your own defined for, I don’t know, type text, whatever you need. And you can then copy that, and store it somewhere. Save it to your template folder. So every time you write a bot, you just add that define to your code, and you’re ready to go. Then if you sell bots, ensure that the lock file is stored not on a specified drive like set or C, use some variables here like the bot directory, or app directory. There’s a function for that, a special folder. You could use that, and then you use special folder.
Or, I just select program file, startup whatever, the application folder. All right? I think you have to add the slash, and then it will write it to that folder. You can check what the result is of that function. So here in my example, so you have to add a slash at the end. Here, it will be in my development dropbox folder, and if you compiled a bot and put it in a C drive or somewhere else, then special folder application will return to the folder where your bot executable assist are.
So that way you can save the lock files to that folder where your bot executable is stored. And then you can save multiple lock files, you can rotate them. Whatever makes sense for your use case. But then you have the data. And you’ll see exactly what click command produces an error, and which one works. And you can even do that for bots you sell. Then if your customers, if they report an error, you just tell them, “Hey, send me that lock file.” Then you can check it and then you’ll see exactly what’s going on. So that way you will have some validation in your command and you added locking capabilities to that. So, that’s it for today. I hope this helps. Wish you a nice day. Talk to you soon. Bye-bye.