如何确定 whatsapp 联系人搜索是否使用 selenium python 找到结果

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

我想使用 selenium 发送 whatsapp python 我从一个 csv 文件中获取我的联系电话 所以 用一个循环 我在联系人搜索框中输入 phone 个号码(WhatsApp 网络) (因为我的一些 phone 联系人是重复的,所以我在搜索框中使用他们的 phone 而不是他们的名字) 并输入 Enter 按钮(当然是硒) 并且它正在进入唯一的结果聊天 这样我就可以发送消息等等

问题是,当搜索号码没有结果时,它会将消息发送给最后一个发送给的人 所以最后一个人得到了重复的消息

我如何确定搜索是否给我任何结果 或者在这种情况下 我怎么知道这个号码有没有whatsapp

谢谢

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:\python\chromedriver')
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")

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)

  • 您可以将最后联系的#存储为变量,并检查消息的当前收件人是否与存储的联系#匹配。
  • 一个简单的 If/Else 应该可以解决问题。

代码

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)