How to determine if whatsapp contact search find results with selenium python

Question:

I want to send whatsapp using selenium python
Im getting my contact numbers from a csv file
So
With a loop
Im typing phone numbers in contact search box (WhatsApp web)
(Because that some of my phone contact are duplicate so I’m using their phone in search box instead of their name)
And entering Enter button (off course with selenium)
And with that it’s entering the only result chat
So i can send the message and etc.

The problem is that when there is no result in searching number it’s sending the messages to the last person that was sent to
So the last person gets duplicate message

How can i determine if the search is giving me any result
Or in this case
How can i know if the number has whatsapp or not

Thanks

from selenium import webdriver
import time
import pandas as pd
import os
import xlrd
import autoit
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException

fileName = 'test.csv'
messages_excel = 'messages.xlsx'
driver = webdriver.Chrome('D:pythonchromedriver')
driver.get('https://web.whatsapp.com/')
input('after QR Code')

with open(fileName) as file:
    data = pd.read_csv(file)
    df = pd.DataFrame(data)

msgdata = pd.read_excel(messages_excel, sheet_name=r'Sheet1')

for index, row in df.iterrows():
    try:
        search_phone = int(row['phone'])
        search_box = driver.find_element_by_class_name('_2zCfw')
        search_box.send_keys(search_phone)
        time.sleep(2)

        search_box.send_keys(u'ue007')

        for i in msgdata.index:
            try:

                clipButton = driver.find_element_by_xpath('//*[@id="main"]/header/div[3]/div/div[2]/div/span')
                clipButton.click()
                time.sleep(1)

                # To send Videos and Images.
                mediaButton = driver.find_element_by_xpath(
                    '//*[@id="main"]/header/div[3]/div/div[2]/span/div/div/ul/li[1]/button')
                mediaButton.click()
                time.sleep(3)

                image_path = os.getcwd() + "\Media\" + msgdata['photoName'][i]+'.jpg'

                autoit.control_focus("Open", "Edit1")
                autoit.control_set_text("Open", "Edit1", (image_path))
                autoit.control_click("Open", "Button1")
                time.sleep(1)
                previewMsg = driver.find_element_by_class_name("_3u328").send_keys(u'ue007')

                time.sleep(3)

                productName = str(msgdata['name'][i])
                oldPrice = str(msgdata['oldqimat'][i])
                newPrice = str(msgdata['newqimat'][i])
                inventory = str(msgdata['inventory'][i])
                msg_box = driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')


                msg_box.send_keys("stocks")
                msg_box.send_keys(Keys.SHIFT + 'ue007')
                msg_box.send_keys(productName)
                msg_box.send_keys(Keys.SHIFT + 'ue007')
                if oldPrice != 'nan':
                    msg_box.send_keys("oldPrice : "+ oldPrice)
                    msg_box.send_keys(Keys.SHIFT + 'ue007')
                if newPrice != 'nan':
                    msg_box.send_keys("newPrice : "+ newPrice)
                    msg_box.send_keys(Keys.SHIFT + 'ue007')
                if inventory!= 'nan':
                    msg_box.send_keys("inventory : "+ inventory)

                time.sleep(1)
                msg_box.send_keys(Keys.ENTER)

                time.sleep(3)
            except NoSuchElementException:
                continue
    except NoSuchElementException:
        continue

print("sucessfully Done")
Asked By: ebrahim khoshnood

||

Answers:

when there is no result in searching number it’s sending the messages to the last person that was sent to So the last person gets duplicate message

Im getting my contact numbers from a csv file So With a loop Im typing phone numbers in contact search box (WhatsApp web)

  • You could store the last # you contacted as a variable and check if the current recipient of the message, matches the stored contact #.
  • A simple If/Else should do the trick.

Code

last_contacted = None

for index, row in df.iterrows():
    try:
        if row['phone'] == last_contacted:
            print("number already contacted")
            next
        else:
            search_phone = int(row['phone'])
            last_contacted = search_phone
            print(search_phone)

Answered By: Anthony R

After you fill the search contact box string and send the Enter key, the “best match” contact name will be displayed at top of the right message panel.
Inspect that element and make sure it matches your search before continuing.

Answered By: Caio Fontana