I want to code whatsapp phone number validator. The script will take input phone numbers list from user and will check over whatsapp if its registered or not

I want to code python script. The script will take input phone numbers list from user and then it will check over whatsapp using selenium if its registered on whatsapp or not. The script will be able to check each and every number from the list if its registered it will display registered if its not it will display not registered .
Please check my code but its not working perfect:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Initialize the webdriver for Firefox

driver = webdriver.Firefox()

# Open the WhatsApp Web interface

driver.get(“https://web.whatsapp.com/”)

# Wait for the user to scan the QR code and log in

input(“Press Enter after scanning the QR code and logging in…”)

# Open the file containing the phone numbers

with open(“phone.txt”, “r”) as f:
for line in f:
# Strip any whitespace from the line
line = line.strip()
# Navigate to the WhatsApp Web search page
driver.get(f"[WhatsApp Web](https://web.whatsapp.com/send?phone=%7Bline%7D)“)
time.sleep(10) # wait for the page to load
# Check if the phone number is registered on WhatsApp
if driver.find_elements_by_xpath(”//button[contains(text(), ‘Send message’)]“):
print(f”{line} - Registered on WhatsApp")
else:
print(f"{line} - Not registered on WhatsApp")

# Close the webdriver

driver.quit()

A couple thoughts for what it is worth:

  • Navigate directly to the search page: “WhatsApp Web” + line
  • You probably want to wait for the search page to load before checking if the phone number is registered on WhatsApp. You can do this using an explicit wait
  • You’ll want to close the chat window before navigating to the next phone number.

I put this all together on a code scratchpad in Einblick so feel free to fork it.

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time

# Initialize the webdriver for Firefox
driver = webdriver.Firefox()

# Open the WhatsApp Web interface
driver.get("https://web.whatsapp.com/")

# Wait for the user to scan the QR code and log in
input("Press Enter after scanning the QR code and logging in…")

# Open the file containing the phone numbers
with open("phone.txt", "r") as f:
    for line in f:
        # Strip any whitespace from the line
        line = line.strip()
        # Navigate to the WhatsApp Web search page
        driver.get("https://web.whatsapp.com/send?phone=" + line)
        wait = WebDriverWait(driver, 10)
        wait.until(EC.presence_of_element_located((By.XPATH, "//div[@class='_3q4NP']")))
        # Check if the phone number is registered on WhatsApp
        if driver.find_elements_by_xpath("//div[@class='_3q4NP']"):
            print(f"{line} - Registered on WhatsApp")
        else:
            print(f"{line} - Not registered on WhatsApp")
        # Click the back button to close the chat window
        back_button = driver.find_element_by_xpath("//span[@data-icon='back']")
        back_button.click()
        time.sleep(1) # wait for the back button to take effect

# Close the webdriver
driver.quit()

Here’s a link to a notebook: Einblick

It seems there are some issues with your code,including incorrect quotation marks and formatting. Here’s a corrected version of your code:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

# Initialize the webdriver for Firefox
driver = webdriver.Firefox()

# Open the WhatsApp Web interface
driver.get("https://web.whatsapp.com/")

# Wait for the user to scan the QR code and log in
input("Press Enter after scanning the QR code and logging in…")

# Open the file containing the phone numbers
with open("phone.txt", "r") as f:
    for line in f:
        # Strip any whitespace from the line
        line = line.strip()
        
        # Navigate to the WhatsApp Web search page
        driver.get(f"https://web.whatsapp.com/send?phone={line}")
        time.sleep(10)  # wait for the page to load

        # Check if the phone number is registered on WhatsApp
        if driver.find_elements_by_xpath("//button[contains(text(), 'Send message')]"):
            print(f"{line} - Registered on WhatsApp")
        else:
            print(f"{line} - Not registered on WhatsApp")

# Close the webdriver
driver.quit()