Automating a WhatsApp bot with Selenium step by step

Last update: 04/12/2025
Author Isaac
  • Automation with Selenium allows control WhatsApp Web as if it were a real user, sending predefined messages to contacts in bulk.
  • A good OOP design (for example, with a WhatsAppBot class) facilitates the encapsulation of logic, code reuse, and bot maintenance.
  • The reliability of the bot depends on correct selectors (XPATH, CSS), managing loading times, and keeping the WhatsApp Web session active.
  • Python C# can be used with Selenium to integrate message sending into larger projects or external automation platforms.

WhatsApp Web automation with Selenium

If you've ever thought “I wish WhatsApp could handle orders on its own.“…shopping lists or reminders without me having to keep an eye on them,” you’re in the right place. More and more developers and curious people are creating their own bot on WhatsApp Web using Selenium, and it’s very common to encounter problems such as… script He says he sent the message… but nothing appears in the conversation.

In the following lines we will see how it works Automating a WhatsApp bot with Selenium, what real uses it has, how a typical project is structured, what common errors can cause the message to "be sent" in the terminal But I didn't get to the chat room, and I'll show you how to build your own bot step by step in both Python and C#. It's all explained in clear, accessible language with practical tips so you can adapt the idea to your own needs.

What is Selenium and why is it useful for automating WhatsApp Web?

Selenium is a browser automation tool It allows you to programmatically control what a human user would do: open a page, click buttons, type in text fields, read information, navigate between sections, and generally simulate real interactions with a website. It's extremely popular in the world of software testing, but it's also widely used to automate repetitive tasks on any website.

when you work with Selenium WebDriverYour script acts as a "remote control" for the browser. You specify which URL to go to, which elements to locate (using XPath, CSS selector, ID, name, etc.), and what action to perform on them: write text, click, submit forms, scroll the page, or extract data. In the case of WhatsApp Web, this capability is used to automate sending messages, managing contacts, or even verifying registration codes.

Applied to WhatsApp Web, Selenium allows you to create bots that write predefined messages to specific contactswithout needing to touch your phone. The browser opens, loads web.whatsapp.com, you log in (usually by scanning the QR code the first time) and from there, the program can open chats, write messages, press the send button and repeat the process with as many contacts as you need.

This approach has one important advantage: You don't depend on official APIs or unsupported external librariesBecause you're simply automating the web version just as you would use it manually. However, you should be aware of potential changes to the WhatsApp Web interface, as changes to the selectors or HTML structure may cause the bot to stop working until you update the code; see [link to relevant documentation]. WhatsApp Web is not loading messages for common solutions.

Main uses of a WhatsApp bot with Selenium

Once you understand how Selenium controls the browser, a huge range of possibilities opens up. A simple bot for WhatsApp Web can start as a proof of concept, but It quickly becomes a useful tool to automate everyday workflows.

  The way to Repair Keyboard Lag in Microsoft Groups

In the field of automated web application testingSelenium is a standard. In the context of WhatsApp, you can use it to simulate how a user interacts with the interface: opening a chat, typing and sending messages, verifying that certain notifications appear on screen, or ensuring that communication flows behave as expected when integrating WhatsApp with other systems.

Another very common use is the automation of repetitive tasksImagine that every day you have to send reminders to a group of clients, confirm appointments, or send standard information. Instead of manually typing it over and over again, the bot handles opening the conversation with each contact and sending a set of predefined messages with the information you choose. You can also take advantage of Automatic replies on WhatsApp Business depending on the case.

The same pattern can be used to build systems of automated communication and marketingYou can manage a contact list and schedule promotional messages, order status updates, or internal company notifications. Although WhatsApp has strict policies, technically the bot acts like a human user, just automated. If you're looking for business-oriented alternatives, consider the automation with WhatsApp Business API.

Finally, we must not forget the part about data collectionWith Selenium, you can read page content, extract message text, record who responds and when, or even combine it with external analytics. While the typical example focuses on sending messages, the same tool allows you to browse third-party websites, gather information, and process it in your script.

Typical structure of a WhatsApp bot with Selenium in Python

Easy Python examples for AI with Scikit-Learn, TensorFlow, and PyTorch.

A very common design for this type of project in Python is based on Programme Object-Oriented Programming (OOP)This encapsulates all the logic for interacting with WhatsApp Web in a specific class. This makes the code cleaner, more modular, and easier to maintain.

The usual thing is to define a class, for example WhatsAppBot, which is responsible for grouping the browser settings, the Boot Selenium provides the framework for opening WhatsApp Web and the methods that will send messages to your contacts. This hides the technical details within the class, so anyone using the bot only needs to create an instance and call its public methods.

Within that class, one method often stands out, such as send_messages (or similar), whose main job is to iterate through a contact list. For each contact, the appropriate WhatsApp Web URL is generated or opened, the message input box is detected using an XPath or other selector, the text is typed, and the pressing of the Enter key or the clicking of the send button is simulated.

The structure is completed with a main block of the type if __name__ == «__main__»where the WhatsAppBot class is instantiated and the method that iterates through the contact list is launched. Thanks to this organization, the script can be reused in other projects or integrated with larger systems without having to modify the internal logic.

This design clearly reflects the principles of OOP: abstraction, encapsulation, and modularityYou abstract the concept of a "WhatsApp bot" into a single class, encapsulate the browser and Selenium details within private or internal methods, and organize the code into reusable pieces so you can adapt the sending logic, messages, or contact source without rewriting everything.

  Learn how to change iPhone backup location on Mac

Execution flow: how the bot works step by step

Behind a script that seems simple lies a very clear execution flow which is important to understand, especially when something goes wrong (for example, when the console says that the message has been sent, but no message appears in the app).

It all starts when an instance of WhatsAppBot (or the class you have defined). In the constructor, Selenium WebDriver is initialized, the browser is configured (usually Chrome, although you can use others) and the necessary options are opened: window size, user profile, implicit timeout, etc.

Once the browser is ready, the method is called that sends messages to the contact listFor each contact, the bot opens the WhatsApp Web URL associated with that number or accesses the chat via internal search. At this point, it's crucial that you're already logged into WhatsApp Web. If it's your first time, you'll have to scan the QR code manually, but from then on, you can reuse the session as long as you don't close the browser or the user profile. If you need to view or close open sessions, check [the relevant section/section/etc.]. View and close open sessions.

The next step is to locate the message input elementA specific XPath or CSS selector is typically used here. Once found, the bot types the predefined text (or several texts) and simulates the sending action, either by pressing the Enter key or clicking the send button. This process is repeated for all contacts in the list.

During execution, the code incorporates exception handling This is in case the text box is missing, the page takes too long to load, or there's a problem with the browser. At the end of the process, the browser and the WebDriver instance are closed, freeing up resources and leaving everything clean for future use.

Technical requirements for automating WhatsApp Web with Python and Selenium

To get a functional project off the ground, you need to meet a series of requirements. basic environment requirementsLuckily, most are easy to set up and only need to be done once.

First, you must have Python 3.x installed on your system. Any modern version usually works well with Selenium, although it's advisable to stay relatively up-to-date to avoid dependency incompatibilities.

The second step is to install the Selenium Library via pip. From the terminal or line of commands simply execute the command pip install seleniumThis will download and install the package needed to control the browser from Python.

In addition to the bookstore, you will need the WebDriver corresponding to your browser (for example, ChromeDriver if you use Google ChromeThis file acts as an intermediary between your Python script and the browser, allowing Selenium to send instructions and receive responses.

Lastly, it is important to have a stable internet connection and have access to WhatsApp Web. Typically, the first time you run the bot, your browser will open to web.whatsapp.com and you'll need to scan the QR code with your phone. Once logged in, the bot can work within that session as long as it remains active.

General steps for using a WhatsApp automation file with Selenium

Once the environment is prepared, the use of an automation script usually follows a set of fairly standard stepswhether you obtained the code from a repository or wrote it yourself.

  How to disable Gaming Copilot in Windows 11 step by step

The first is Download the file with the source code or clone the repository where the project is located. In many cases, you'll have a main file like main.py, which will be the application's entry point.

Then you open your favorite editor or IDE (VS Code, PyCharm, etc.) and create, if necessary, the .py file where you will copy the code. If it's already in a repository, simply open it from the IDE to modify it as you wish.

Within the script you will find a section where the contact listIt is usually a structure like contacts = . That's where you have to add the people you want to send messages to, respecting the format defined by the original developer.

You will also have a list or block with the predefined messages that the bot will send. It's a good idea to customize these texts to suit the actual use case: order confirmations, delivery notifications, reminders, etc. By changing this list, you can easily vary the content that will be sent to all contacts.

Once you have configured contacts and messages, you save the file and You run it from the terminal or from the IDE itselfThe browser will open, load WhatsApp Web, and after making sure you are logged in, you will see how the bot automates the process of sending messages to each contact on the list over and over again.

Example Python project: main.py and customization

Many repositories that show how to automate WhatsApp Web with Selenium are structured around a main file, usually called main.pywhich contains the bot's basic logic. You can then make adjustments and improvements based on that, according to your needs.

In this type of project, the main file is usually responsible for load the contact listInitialize the bot (the instance of the class that controls Selenium) and launch the method that sends the messages. The contact list can be hardcoded directly into the script or come from an external file, but in many examples it is presented directly in the code for simplicity.

If you want to change the behavior, simply edit main.py to add or remove contacts, modify the texts to be sent or even introduce new functions, such as recording in a log which messages have been successfully sent and at what time, or managing waiting lists for retries.

This approach makes experimenting really easy: You can start with a minimal bot It only sends one or two messages to a couple of test numbers, and then you can gradually make the logic more complex. As you gain confidence, you can integrate the bot with databasesexternal systems or third-party APIs that automatically generate message content; for example, learning to Create a chatbot using the ChatGPT API It can be useful for generating dynamic responses.

Furthermore, the fact that many projects are open source encourages anyone to participate. contribute with improvements, corrections or new featuresIf you find a more robust way to select items, better manage wait times, or add support for more browsers, you can share those changes to help other developers.

Waiting for message. This may take some time on WhatsApp.
Related article:
“Waiting for message. This may take some time” on WhatsApp: what it means and how to fix it