M.Colosso
M.Colosso

Reputation: 49

How to send a pdf file with Whatsapp Web using Selenium and Python in Windows 10

I am trying to send some payment receipts via Whastapp Web, as I have done via e-mail, but I can not specify the name of the file to send and press 'Enter' in the Windows common control dialog. I know that it is not possible to do it directly with Selenium and I've tried with win32gui, without success.

This is the code I've been using to test:

import win32gui
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

send_to = 'John Smith'
attach_name = "GyG Recibo_00741.pdf"

# Lets use Chrome as web browser
web_driver = webdriver.Chrome('ChromeDriver')

# Open WhatsApp Web
web_driver.get('http://web.whatsapp.com')
web_driver.implicitly_wait(100)   # seconds

# Find destinatary
new_chat = web_driver.find_element_by_id('input-chatlist-search')
new_chat.send_keys(send_to, Keys.ENTER)

# Press Attach button
button = web_driver.find_elements_by_xpath('//*[@id=\"main\"]/header/div[3]/div/div[2]/div/span')
button[0].click()

# Press Document button
inp_xpath = '//*[@id=\"main\"]/header/div[3]/div/div[2]/span/div/div/ul/li[3]/button'
button = web_driver.find_elements_by_xpath(inp_xpath)
button[0].click()

# Loop until Open dialog is displayed (my Windows version is in Spanish)
hdlg = 0
while hdlg == 0:
    hdlg = win32gui.FindWindow(None, "Abrir")

# Set filename and press Enter
hwnd = 0x000047C   # ControlID (as reported by Spy++)
filename = attach_name
ent = "{ENTER}"
win32gui.SetDlgItemText(hdlg, hwnd, filename)
win32gui.SetDlgItemText(hdlg, hwnd, ent)

Upvotes: 2

Views: 5797

Answers (1)

M.Colosso
M.Colosso

Reputation: 49

Finally, I found the way... We have to navegate in the Open dialog to the edit field to place the filename and press the Save button:

# Loop until Open dialog is displayed (my Windows version is in Spanish)
hdlg = 0
while hdlg == 0:
    hdlg = win32gui.FindWindow(None, "Abrir")

time.sleep(1)   # second. This pause is needed

# Set filename and press Enter key
hwnd = win32gui.FindWindowEx(hdlg, 0, 'ComboBoxEx32', None)
hwnd = win32gui.FindWindowEx(hwnd, 0, 'ComboBox', None)
hwnd = win32gui.FindWindowEx(hwnd, 0, 'Edit', None)

filename = attach_path + attach_name
win32gui.SendMessage(hwnd, win32con.WM_SETTEXT, None, filename)

# Press Save button
hwnd = win32gui.FindWindowEx(hdlg, 0, 'Button', '&Abrir')

win32gui.SendMessage(hwnd, win32con.BM_CLICK, None, None)

Upvotes: 3

Related Questions