Making a WhatsApp Spammer in under 10 lines of Python

Welcome to Anarchy 101.

In today’s class, we’ll learn how to send an unlimited number of messages to a group or person from your WhatsApp account, and we’re going to do it in under ten lines of code.

Why restrict ourselves to ten lines, you ask? Because we can. Moving on.

This code is written in Python and using Selenium. Selenium is a web automation framework used for testing purposes (… well, most of the time). You can read more about it here.

I’ll walk you through the code line by line, explaining each part of it, so you know why you’re doing what you’re doing.

Before we start, here’s a glimpse of what you can look forward to once you’re done:

Anarchy, Exhibit A

Nothing says aggressive like 100+ messages a minute.

Alright then, here’s your code for the day:

from selenium import webdriver
driver = webdriver.Chrome('C:\\webdrivers\\chromedriver.exe')
driver.implicitly_wait(15)
driver.get('https://web.whatsapp.com')
driver.find_element_by_css_selector("span[title='" + input("Enter name to spam: ") + "']").click()
inputString = input("Enter message to send: ")
while(True):
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]').send_keys(inputString)
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[3]/button').click()

Ah, yes. Gorgeous. A weapon of mass communication.

Don’t let the disgusting structure of the code intimidate you, let’s walk through it line by line.

1.

from selenium import webdriver

The first line is an import. We can’t use a part of Selenium unless we import them, right? Here, we’re importing “webdriver”, which is something that allows your code to interact with a browser like a human does.

2.

driver = webdriver.Chrome('C:\\webdrivers\\chromedriver.exe')

The second line is us specifying where we’re getting our “driver” from. This driver is a file that contains instructions that let you interact with the browser, specifically Chrome browser in this case, and you can get it from anywhere on the internet. For example, you’ll find the chrome webdriver here.

Once you find the webdriver and download it, you’ll want to copy its path and paste it over where it currently says “C:\\webdrivers\\chromedriver.exe”
(Note the double backslashes to escape a single backslash)

3.

driver.implicitly_wait(15)

The third line tells our driver to implicitly wait for 15 seconds. What does that mean?

See, when we ask Selenium to get an element from a page for us, if the element is not found on the page, an exception is thrown, which stops your code in its tracks if you haven’t handled it well. But an element can be missing from a page for many reasons. The page could still be loading, for example.

With the third line of code, we’re telling our driver to look for an element for at least 15 seconds before reporting it rogue to us. That’s all. You can increase this timer if you have an even slower internet connection. There’s no need to decrease the timer however, because the moment the element is found, the code will proceed.

4.

driver.get('https://web.whatsapp.com')

The fourth line of code simply starts a new instance of our browser through our driver. This is where we’ll mess with Whatsapp. Make sure to start your links with “http” (and “https” where available).

Next up, we need to start looking for identifiers for elements on our web page. We need a way of uniquely identifying each element, so we can tell our code to look for it. How do we do that? Visit the page, right click on the element that you want to single out, and hit “inspect element” (it likely won’t work for WhatsApp Web, as a right click over there opens their custom context menu, not the standard one, in which case…), alternatively hit Ctrl + Shift + i (Windows) or Command + Option + i (Mac). This opens the developer console window.

The shortcut keys may vary from browser to browser, but it’s a short web search away.

If you opened the console by hitting the shortcut keys, you’ll need to manually find your element. First, switch the tab on top to “Elements” if that’s not already selected.

Then, hover over each line of code, which will highlight its corresponding element on the page. At first, it will seem like everything is highlighted. That is normal. You’ll notice you can expand that line of code. Expand it, and move lower. Keep going deeper and deeper into the code until you find a line that highlights just the element you want to click. From there, you can see its id, if it has one, its class etc., and you can right click on that line of code to find the elements’ XPath, selector etc.

For the purpose of this tutorial, all of this is unnecessary, as I’ve already found the required information, and at the time of writing this post, these paths work just fine.

5.

driver.find_element_by_css_selector("span[title='" + input("Enter name to spam: ") + "']").click()

Now, the fifth line of code is what we use to open our victim’s chatbox. Within the function call for finding an element by its CSS Selector, we take input from the terminal. The input we’re looking for here is the victim’s name. Once this input is given, the corresponding chatbox will be clicked on WhatsApp Web.

This is what that will look like:

I fouuuund youuuuu

6.

inputString = input("Enter message to send: ")

The sixth line is just to get input for the message we’re going to spam our victim with, and stores it in inputString.

This will look like that will look like:

One small message for me, lots of small messages for youkind

7.

while(True):

The seventh line of code starts an infinite while loop, and you can modify this according to your taste. Don’t want to end up harassing them for too long now, do we?

8.

driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]').send_keys(inputString)

In the eighth line of code, we ask our driver to locate the little text field where we generally type our messages (based on its XPath that I extracted). Once it finds that field, it will send text to it, according to the argument provided in send_keys().

In this case, it’s our inputString.

9.

driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[3]/button').click()

Once the text is sent, we are ready to hit the send button. Similar to the 8th line, the ninth line locates the send button, and clicks on it using the click() method.

That’s it! That’s all there is to it! You’ve successfully written your very own WhatsApp Spammer in under ten lines of code. Save this as a .py file, and run it. When you do, a terminal will pop up (that you will interact with to provide input), and shortly after, a browser will pop up, possibly with the notification that it’s being run by automated test software.

The browser will navigate to Whatsapp Web, where you will have to scan the QR code with your phone. You will have to do this regardless of whether you’re already signed into WhatsApp or not.

You’ll notice the console now asks you for the name of the person you want to spam. Type it in and hit enter. Right after this, the console will ask you to enter the message you want to spam out. Type that in and hit enter as well, and BAM, let the code do the rest of the work for you.

Of course, if you’re using the infinite loop version, just terminate the browser to make it stop when you’ve had enough.

And yes, make sure you’re using this with caution, because, well, people don’t like it very much.

Ouch

Let me know if you run into any issues in the comments, and I’ll be glad to help out.

Class dismissed.

--

--