Python "Hello Web Bot"¶
Warning
This tutorial uses a old version of the project template offered by BotCity.
If you've used this template before and have any questions, you can continue using this guide as normal.
If you are starting with Python automations, we strongly recommend using the new version of the project template.
This tutorial will guide you through the process of creating a simple Python web automation.
Prerequisites¶
- BotCity Account
- BotCity Studio SDK
- Python 3.7 or higher
Downloading the WebDriver¶
To be able to work with web automations, we need to use the corresponding WebDriver to communicate with browsers for automation.
In doing so, it requires that the WebDriver for the chosen browser to be installed and available preferrably
in your PATH
. If you can't add the necessary WebDriver to your PATH
you will be able to inform the driver path
via code in your bot.
Here is a list of supported browsers along with links for you to download the proper WebDriver:
Browser | WebDriver Download |
---|---|
Chrome | ChromeDriver |
Firefox | GeckoDriver |
Edge | MSDriverEdge |
IE | IEDriverServer |
Please follow the instructions on the WebDriver website for installation and setup.
Once the desired WebDriver for the Web Browser to be used is installed we can proceed to the next steps.
Creating Your First Project¶
BotCity offers a template project which can be customized via a tool called cookiecutter.
Installing Cookiecutter¶
In order to use it we need first to install the cookiecutter Python package by running the following command on your command-line terminal:
python -m pip install --upgrade cookiecutter
After doing that you are ready to create your first Python web automation using BotCity’s framework.
From Template to Project¶
With cookiecutter properly installed, it is time to make use of it and create a new project.
To create a new project using our template we will invoke the cookiecutter and provide as argument the URL in which the templates from BotCity is located:
The system will prompt you with a couple of answers in order to properly create your project.
- Once prompted for project_type answer with 2 for Web and press enter;
- For bot_id type
HelloWebBot
and press enter; - Under project_name type
Hello Web Bot
and press enter; - For project_short_description type
My first project with BotCity
and press enter.
Info
More information about the project types and options can be found on the Template project documentation website.
After going through the process above you will now have a new folder named HelloWebBot
.
Success
Congratulations, you now have a project with BotCity’s Python Web Framework. 🏆
Let’s take a deeper look into it.
Exploring the Project¶
Under your project folder HelloWebBot
you will have the following structure:
HelloWebBot
├── MANIFEST.in <- This file defines the content of the package.
├── README.md <- Simple README file for your bot project.
├── VERSION <- This file defines the Bot package version.
├── HelloWebBot <- Main module for your Bot package.
│ ├── __init__.py
│ ├── __main__.py <- Entrypoint for the module.No need to bother with this file.
│ ├── bot.py <- Here is where you will develop your bot code.
│ └── resources <- Folder containing resources useful for the Bot.
├── build.bat <- Batch script to generate the package
├── build.sh <- Shell script to generate the package
├── requirements.txt <- File describing the python dependencies for your Bot.
└── setup.py <- Setup file for the package.
Note
It may seem like a lot of files and folders but here are the most important ones:
- bot.py: Change this file and add here the code for your bot.
- resources: Add into this folder files to be used with your bot such as images, spreadsheets and etc.
- VERSION: Change the content of this file when updating the version of your bot. It is recommended to use versions in the format X.Y. E.g. 1.0, 1.1, 2.5, 3.10.
Great!
All this information is great but it is time to see some action.
Let’s test this shiny new Bot locally. 🦾🤖
Setting WebDriver Path¶
As mentioned before, to be able to use the web framework features, it is necessary to configure the WebDriver path of the browser we are using.
If you don't have the WebDriver path added to your PATH
, you can manually add the path in your bot code, via the driver_path
property.
...
class Bot(WebBot):
def action(self, execution=None):
# Configure whether or not to run on headless mode
self.headless = False
# Changing the default Browser to Firefox
self.browser = Browser.FIREFOX
# Setting the path of the Firefox WebDriver
self.driver_path = "<path to your WebDriver binary>"
# Opens the BotCity website.
self.browse("https://www.botcity.dev")
...
Info
Chrome is set as the default browser. To change the default browser, just select another option through the Browser
class.
Testing Your Project Locally¶
In order to test our project locally, let’s first install it so we can have all the Python dependencies ready.
Using the command-line tool, access the HelloWebBot
folder which we described above.
Installing the Project¶
From this folder run the command below to install your project on development mode:
Pro Tip
Installing using the development mode flag -e
makes it so that we can keep developing and running our Bot without the need to reinstall it.
This command will produce a lot of output which means that all dependencies such as botcity-framework-web
, and others are being installed.
Once this process is over, you should see an output similar to this one:
<... other output from installation ...>
Running setup.py develop for HelloWebBot
Successfully installed HelloWebBot-1.0 ...
The important message here is the Successfuly installed HelloWebBot-1.0
.
This means that your project is now ready to be executed.
Running the Bot¶
Our template project runs a very simple automation. It opens up the web browser you have configured and loads BotCity’s website.
You can execute your HelloWebBot
with the following command:
Here is a screenshot of the expected result:
🌟 Excellent 🌟
You are now ready to start creating automations using the BotCity’s Python Web Framework.
Conclusion¶
Under this tutorial you learned:
-
The dependencies required to develop automations using BotCity’s Python Web Framework and how to get them installed.
-
How to create new WebBot projects using cookiecutter and BotCity’s template.
-
How to install and run your new WebBot project locally.
Have fun automating 🤖
Next Steps¶
Now it is time to load your project with BotCity Studio and start creating your automations with Computer Vision and all the productivity offered by our tool.