首页 > 解决方案 > web.whatsapp 没有传递机器人的消息

问题描述

我创建了一个机器人来通过 web.whatsapp 发送消息。机器人打开浏览器,打开与联系人聊天,写消息,但消息未送达

我正在使用 python 2、selenium 和 chrome.webdriver


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

message_text='Ola essa é uma mensagem automatizada de Whats App escrita pelo computador e enviada em uma campanha massiva de marketing. Obrigado pela atenção' # message
no_of_message=3 # no. of time 
moblie_no_list=[xxxxxxxxx] # list of phone number 

def element_presence(by,xpath,time):
    element_present = EC.presence_of_element_located((By.XPATH, xpath))
    WebDriverWait(driver, time).until(element_present)

def is_connected():
    try:
        # connect to the host -- tells us if the host is actually
        # reachable
        socket.create_connection(("www.google.com", 80))
        return True
    except :
        is_connected()
driver = webdriver.Chrome(executable_path="chromedriver.exe")
driver.get("http://web.whatsapp.com")
sleep(10) #wait time to scan the code in second

def send_whatsapp_msg(phone_no,text):
    driver.get("https://web.whatsapp.com/send?phone={}&source=&data=#".format(phone_no))
    try:
        driver.switch_to.alert().accept()
    except Exception as e:
        pass


    sleep(5)
    txt_box=driver.find_element(By.XPATH , '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
    txt_box.click()
    txt_box.send_keys("oi")
    sleep(2)
    txt_box.send_keys(Keys.ENTER)


for moblie_no in moblie_no_list:
    try:
        send_whatsapp_msg(moblie_no,message_text)

    except Exception as e:
        sleep(10)
        is_connected()

https://ibb.co/7XWQxvr

标签: pythonseleniumwhatsapp

解决方案


推荐阅读