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 selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by 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")
options.add_argument("--profile-directory=Default")
options.add_argument("--window-size=1920,1080")
options.add_argument('--ignore-certificate-errors')
options.add_argument('--allow-running-insecure-content')
options.add_argument("--disable-extensions")
options.add_argument("--proxy-server='direct://'")
options.add_argument("--proxy-bypass-list=*")
options.add_argument("--start-maximized")
options.add_argument('--disable-gpu/')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--no-sandbox')
options.add_argument("--log-level=3")
options.add_argument('--hide-scrollbars')
options.add_argument("--disable-popup-blocking")
options.add_experimental_option("excludeSwitches",["enable-automation"])
options.add_experimental_option("useAutomationExtension",False)

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

# Opening the browser window
driver.get('https://web.whatsapp.com/')
options.add_argument('--user-agent={}'.format(user_agent))

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

# 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"]')))
search_box.send_keys(my_number_saved_name)
time.sleep(3)

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


for number_index in range(len(number_list)):
    try:
        if number_list[number_index] == 0:
            continue
        # Click on the message box
        message_box = driver.find_elements(By.XPATH,'//div[@title="Type a message"]')
        message_box[0].send_keys(f'wa.me/{country_code}{number_list[number_index]}')

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

        # 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]
        last_link.click()
        time.sleep(2)

        try:
            # Checking for Invalid POP
            if driver.find_element(By.XPATH,'//div[@data-animate-modal-popup="true"]'): 
                driver.find_element(By.XPATH,'//*[@id="app"]/div/span[2]/div/span/div/div/div/div/div/div[2]/div/button/div/div').click()
                time.sleep(1)
                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"]')))
        search_box.send_keys(my_number_saved_name)
        time.sleep(2)

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

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

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

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.

Disclaimer:

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 *