Open whatsapp on a headless browser

1.6k Views Asked by At

Use case:

Sending out automated WhatsApp messages using pythonanywhere. Step by step logic below:

  1. non-coders write on a gsheet the phone numbers to which we should send out the messages

  2. the gsheet data is read (using gspread in pythonanywhere)

  3. open WhatsApp URL to send out the messages in bulk

I have a code using selenium running on my machine that opens the web whatsapp url, finds the needed elements on the website, and sends the messages to the gsheets phone numbers - find below a snippet from that part of the code that I am using on my machine:

global driver
driver.get('https://web.whatsapp.com/')
waiter.until(EC.title_is("WhatsApp"))
waitCounter = 0
while 1:
    try:
        waiter.until(EC.presence_of_element_located((By.XPATH, "//canvas[@aria-label='Scan me!']")))
        waitCounter+=1
        if waitCounter%1000 == 0:
            print("Waiting for user to log in...", 'WARNING')
    except:
        print("Logged in to WhatsApp")
        break

for entry in data:
    driver.find_element_by_xpath(PHONE_NUMER_INPUT).send_keys(str(entry['PhoneNumber']))
    time.sleep(2)
    driver.find_element_by_xpath(PHONE_NUMER_INPUT).send_keys(Keys.ENTER)
    time.sleep(2)
    driver.find_element_by_class_name('p3_M1').send_keys(str(entry['Message'])) 
    time.sleep(2)
    driver.find_element_by_class_name('_4sWnG').click()
    time.sleep(2)

Doubt:

To make step nr 3. working on python anywhere I would have to use a headless browser. However, to initiate whatsapp web we always need to do the QR code scan, so I am not being able to do it that way. Find below the current (useless) part of my code with the headless selenium code - (NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id='side']/div[1]/div/label/div/div[2]"}). I am quite stuck here. Any tip or idea to overcome this is more than welcome and happy to discuss any possible solutions using other libraries that you guys might find appropriate.

Thanks in advance.

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
driver = webdriver.Chrome(options=chrome_options) 


def send_whatsapp_message():
    global driver
    driver.get('https://web.whatsapp.com/')
    print("Done updating, check the spreadsheet now")
    #redirect('https://web.whatsapp.com/', code=302)

    for entry in data:
        driver.find_element_by_xpath("//*[@id='side']/div[1]/div/label/div/div[2]").send_keys(str(entry['PhoneNumber']))
        time.sleep(2)
        driver.find_element_by_xpath("//*[@id='side']/div[1]/div/label/div/div[2]").send_keys(Keys.ENTER)
        time.sleep(2)
        driver.find_element_by_class_name('p3_M1').send_keys(str(entry['Message']))
        time.sleep(2)
        driver.find_element_by_class_name('_4sWnG').click()
        time.sleep(2)
        print("Successfully send message to {0}, name: {1}".format(str(entry['PhoneNumber']), str(entry['Name'])), 'INFO')
0

There are 0 best solutions below