-
Notifications
You must be signed in to change notification settings - Fork 0
/
whatsapp_api.py
101 lines (83 loc) · 3.15 KB
/
whatsapp_api.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- coding: utf-8 -*-
"""
Created on Thu Jun 25 08:22:38 2020
@author: Roberto de Souza
"""
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.options import Options
from time import sleep
# Parameters
WP_LINK = 'https://web.whatsapp.com'
## XPATHS
CONTACTS = '//*[@id="main"]/header/div[2]/div[2]/span'
SEND = '//*[@id="main"]/footer/div[1]/div[3]'
MESSAGE_BOX = '//*[@id="main"]/footer/div[1]/div[2]/div/div[2]'
NEW_CHAT = '//*[@id="side"]/header/div[2]/div/span/div[2]/div'
FIRST_CONTACT = '//*[@id="app"]/div/div/div[2]/div[1]/span/div/span/div/div[2]/div/div/div/div[2]/div'
SEARCH_CONTACT ='//*[@id="app"]/div/div/div[2]/div[1]/span/div/span/div/div[1]/div/label/div/div[2]'
class WhatsApp:
def __init__(self):
self.driver = self._setup_driver()
self.driver.get(WP_LINK)
print("Please scan the QR Code")
@staticmethod
def _setup_driver():
print('Loading...')
chrome_options = Options()
chrome_options.add_argument("disable-infobars")
driver = webdriver.Chrome(chrome_options=chrome_options)
return driver
def _get_element(self, xpath, attempts=5, _count=0):
'''Safe get_element method with multiple attempts'''
try:
element = self.driver.find_element_by_xpath(xpath)
#print('Found element!')
return element
except Exception as e:
if _count<attempts:
sleep(1)
#print(f'Attempt {_count}')
self._get_element(xpath, attempts=attempts, _count=_count+1)
else:
print("Element not found")
def _click(self, xpath):
el = self._get_element(xpath)
el.click()
def _send_keys(self, xpath, message):
el = self._get_element(xpath)
el.send_keys(message)
def write_message(self, message):
'''Write message in the text box but not send it'''
self._click(MESSAGE_BOX)
self._send_keys(MESSAGE_BOX, message)
def _paste(self):
el = self._get_element(MESSAGE_BOX)
el.send_keys(Keys.SHIFT, Keys.INSERT)
def send_message(self, message):
'''Write and send message'''
self.write_message(message)
self._click(SEND)
def get_group_numbers(self):
'''Get phone numbers from a whatsapp group'''
try:
el = self.driver.find_element_by_xpath(CONTACTS)
return el.text.split(',')
except Exception as e:
print("Group header not found")
def search_contact(self, keyword):
'''Write and send message'''
self._click(NEW_CHAT)
self._send_keys(SEARCH_CONTACT, keyword)
sleep(1)
try:
self._click(FIRST_CONTACT)
except Exception as e:
print("Contact not found")
def get_all_messages(self):
all_messages_element = self.driver.find_elements_by_class_name('_12pGw')
all_messages_text = [e.text for e in all_messages_element]
return all_messages_text
def get_last_message(self):
all_messages = self.get_all_messages()
return all_messages[-1]