Automating WhatsApp Number Verification using Selenium

Automating WhatsApp Number Verification using Selenium

Python Python Selenium

Introduction: WhatsApp is a widely used messaging application, and at times, it becomes essential to verify whether a given phone number is registered on WhatsApp. In this context, we aim to check the validity of phone numbers, distinguishing between those that are invalid and valid. This article delves into an automation script utilizing Selenium for WhatsApp number verification. The script is crafted to log into WhatsApp Web, search for a specified contact, and assess the validity of a list of phone numbers.

WhatsApp Number Verification Using Python

Script Overview: The script utilizes the Selenium WebDriver along with ChromeOptions to configure the browser settings. The headless mode is enabled to run the browser without a graphical interface, making it suitable for automation. The user’s Chrome profile directory is specified for persistence.

The script then navigates to the WhatsApp Web URL, where it prompts the user to scan the QR code for authentication. Once logged in, it waits for the WhatsApp interface to load.

Searching for Contacts: The script begins by searching for a specific contact using the provided ‘my_number_saved_name.’ It locates the search box and enters the contact name. After a short delay, it clicks on the contact name to open the chat.

Number Verification Loop: The script iterates through a list of phone numbers stored in ‘number_list.’ For each number, it constructs a WhatsApp link and sends it as a message to the contact. It then waits for a response by clicking on the last link in the chat.

If the script identifies a pop-up indicating an invalid number, it logs the information and continues to the next number. Otherwise, it confirms the validity of the number.

WebDriver Options: The script sets up various WebDriver options, including window size, user agent, and other settings necessary for smooth execution. These options enhance the performance and reliability of the automation process.

from selenium import webdriver
from import expected_conditions as EC
from import WebDriverWait
from import Options
from import By
import time, getpass

Title: WhatsApp Number Verification
Author: Danish Ali
Organization: MasterProgramming
Description: The purpose of WhatsApp number verification is to validate whether a provided phone number is eligible for use with the WhatsApp messaging application.

# Enter Your Current WhatsApp Number Name or Number
my_number_saved_name = "My Jio (You)"
country_code = "91"
number_list = ['0000000000', '853486776', '8534867764', '853486776', '8126790999','8126790']

# ALL Selenium Required Options for WebDriver
options = Options()
options.headless = True
options.add_argument(f"--user-data-dir=C:/Users/{getpass.getuser()}/AppData/Local/Google/Chrome/User Data/Default")

# Setup the Chrome Driver Object
driver = webdriver.Chrome(options=options)
user_agent = driver.execute_script("return navigator.userAgent;")

# Opening the browser window

# Scan the QR Code
print(f"Scan Your QR Code Or Wait Until Logged In " + '\n')

# Wait until whatsapp logged In
wait = WebDriverWait(driver, 1200)
reset = wait.until(EC.visibility_of_element_located((By.XPATH,'//*[@id="pane-side"]')))

# Click on Text Box
search_box = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@title="Search input textbox"]')))

# Click on Search input textbox
search_number = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//span[text()="(You)"]/parent::div')))

for number_index in range(len(number_list)):
        if number_list[number_index] == 0:
        # Click on the message box
        message_box = driver.find_elements(By.XPATH,'//div[@title="Type a message"]')

        # Click on Send Button
        send_button = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.XPATH,'//span[@data-icon="send"]')))

        # Click on the Last Link in the Chat
        last_link = driver.find_elements(By.XPATH, '//*[@id="main"]/div/div/div/div/div/div/div/div/div/div/div/div/div/span/span/a')[-1]

            # Checking for Invalid POP
            if driver.find_element(By.XPATH,'//div[@data-animate-modal-popup="true"]'): 
                print(f"[-] Number is Invalid : {country_code}{number_list[number_index]}")
        except Exception as e:
            print(f"[+] Number is Valid : {country_code}{number_list[number_index]}")
        # Click on Text Box
        search_box = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//*[@title="Search input textbox"]')))

        # Click on Search input textbox
        search_number = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH,'//span[text()="(You)"]/parent::div')))

    except Exception as e:
        print("Something went wrong {e}")

print("Thanks for using WhatsApp Number Verification Service.")

Conclusion: Automating WhatsApp number verification can be a valuable tool for individuals or organizations dealing with a large volume of phone numbers. The script presented here provides a foundation for such automation, allowing users to efficiently verify the validity of WhatsApp numbers.

Keep in mind that while automation scripts like these can be helpful, it’s crucial to use them responsibly and ethically, respecting privacy and adhering to relevant terms of service. Automation should always be conducted with a clear understanding of the platform’s policies to avoid any unintended consequences.


The creators of this script and the platform providing it do not endorse or encourage any misuse of this script for malicious purposes. Users are solely responsible for their actions and must use this script in compliance with legal and ethical standards.

Thank you for your commitment to responsible and ethical automation.

Leave a Reply

Your email address will not be published. Required fields are marked *