Skip to content

Python "Hello Web Bot"

This tutorial will guide you through the process of creating a simple Python web automation.

Prerequisites

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:

python -m cookiecutter https://github.com/botcity-dev/bot-python-template/archive/v2.zip

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;

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
├── 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.ps1        <- PowerShell script to generate the package.
├── build.sh         <- Shell script to generate the package.
├── requirements.txt <- File describing the python dependencies for your Bot.
└── HelloWebBot.botproj <- File used to load the project on BotStudio.

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.
  • requirements.txt: Change this file and add all external dependencies that are used in the code.

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.

...
def main():
    bot = WebBot()

    # Configure whether or not to run on headless mode
    bot.headless = False

    # Changing the default Browser to Firefox
    bot.browser = Browser.FIREFOX

    # Setting the path of the Firefox WebDriver
    bot.driver_path = "<path to your WebDriver binary>"
...

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

To test your project locally, let's first install the Python dependencies that are being used in the project.

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 the dependencies defined in the requirements.txt file:

pip install --upgrade -r requirements.txt

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 complete, your project is ready to run.

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.

Using the open terminal inside the project folder, you can run your HelloWebBot automation with the following command:

python bot.py

Here is a screenshot of the expected result: Python Web 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.