skkrish
skkrish

Reputation: 288

Whatsapp Automated Bot not able to search in WhatsApp Contact List

I'm trying to implement a WhatsApp bot, which uses the chromedriver and opens whatsapp web, and sends messages to the contacts. These are the steps of the program:

Here are the problems that I'm unable to solve:

  1. If the name is not there in recent chats, it is not able to search it from the WhatsApp contact list and send
  2. Also I want to know how I can send images and videos

Since the code is pretty long, here is the link to it: https://github.com/harshitsidhwa/WhatsApp-bot-selenium

Upvotes: 7

Views: 10181

Answers (2)

Shaurya Uppal
Shaurya Uppal

Reputation: 3680

Python sending messages to contacts:

def send_message(target):
global message,wait, browser
try:
    x_arg = '//span[contains(@title,' + target + ')]'
    group_title = wait.until(EC.presence_of_element_located((By.XPATH, x_arg)))
    group_title.click()
    input_box = browser.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]')
    input_box.send_keys(message + Keys.ENTER)
    time.sleep(1)
except NoSuchElementException:
    return

Here the variable target is the contact name whom you want to send a message. Variable message contains the text message you want to send to that contact. Variable browser is web driver variable.

I have already worked on WhatsApp Automation, in case of any help refer to the link: https://github.com/shauryauppal/PyWhatsapp

You have stated two problems:

  1. You cannot access to contact names by contact list or archived contact list. The only way is recent chat contacts. For other contacts, you have to visit all contacts list select the target person then send messages. (Will implement soon).

  2. Sending of Images, Files and Videos can be Implemented by PyAutoIt. Refer my repo, I have already implemented that.

Step1: AutoIt.exe Installation Link

Step2:

pip install PyAutoIt

Refer my code or this link for guidance.

Upvotes: 1

Sers
Sers

Reputation: 12255

Basic example Java code to search contact, send message and/or file. You can implement explicit wait if need.

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

List<String> targets = new ArrayList<>();

for (String target:targets) {
    driver.findElement(By.xpath("//button[.//span[@data-icon='search']]/..//input")).clear();
    driver.findElement(By.xpath("//button[.//span[@data-icon='search']]/..//input")).sendKeys(target);
    try {
        driver.findElement(By.cssSelector("span[title='"+target+"']")).click();
    } catch (WebDriverException e) {
        System.out.println("Not found");
        continue;
    }
    driver.findElement(By.cssSelector("div[contenteditable='true']")).sendKeys("MassageToSend");
    driver.findElement(By.cssSelector("span[data-icon='send']")).click();

    //To send attachments
    //click to add
    driver.findElement(By.cssSelector("span[data-icon='clip']")).click();
    //add file to send by file path
    driver.findElement(By.cssSelector("input[type='file']")).sendKeys("FilePath");
    //click to send
    driver.findElement(By.cssSelector("span[data-icon='send-light']")).click();
}

In python (may contains syntax errors):

if msgToSend[count][0]==curHour and msgToSend[count][1]==curMin and msgToSend[count][2]==curSec:
    # utility variables to tract count of success and fails
    success = 0
    sNo = 1
    failList = []

    # Iterate over selected contacts
    for target in targets:
        print(sNo, ". Target is: " + target)
        sNo+=1

        inputSearchBox = driver.find_element_by_id('//button[.//span[@data-icon="search"]]/..//input')
        inputSearchBox.clear()
        inputSearchBox.send_keys(target[1:len(target) - 1])

        try:
            wait5.until(EC.presence_of_element_located((By.CSSSELECTOR, 'span[title="'+ target +'"]')))
        except:
            print("Cannot find Target: " + target)
            failList.append(target)
            continue

        driver.find_element_by_css_selector('div[contenteditable="true"]').sendKeys("Hello, " + target + "."+ Keys.SHIFT + Keys.ENTER + msgToSend[count][3] + Keys.SPACE)
        driver.find_element_by_css_selector('span[data-icon="send"]').click()

        # To send attachments
        # click to add
        driver.find_element_by_css_selector('span[data-icon="clip"]').click()
        # add file to send by file path
        driver.find_element_by_css_selector('input[type="file"]').sendKeys("FilePath")
        # click to send
        driver.find_element_by_css_selector('span[data-icon="send-light"]').click()

Upvotes: 0

Related Questions