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

2024-04-30 16:47:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用seleniumpython发送whatsapp 我从csv文件中获取我的联系号码 所以 有一个循环 Im在联系人搜索框中键入电话号码(WhatsApp web) (因为我的一些电话联系人是重复的,所以我在搜索框中使用他们的电话,而不是他们的姓名) 进入回车键(使用selenium偏离航向) 然后它就进入了唯一的结果聊天 所以我可以发信息等等

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

我怎样才能确定搜索是否有结果 或者在这种情况下 我怎么知道这个号码有没有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")

Tags: importdivboxsendsearchtimedrivermsg
1条回答
网友
1楼 · 发布于 2024-04-30 16:47:12

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)

相关问题 更多 >