-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWhatsapp_Automation_Basic.py
53 lines (36 loc) · 1.78 KB
/
Whatsapp_Automation_Basic.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
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
import pyperclip
from selenium.webdriver.common.keys import Keys
driver=Service(executable_path='C:\Drivers\chromedriver_win32\chromedriver.exe')
brw=webdriver.Chrome()
brw.maximize_window()
brw.get('https://web.whatsapp.com/')
with open ('Whatsapp_Group.txt','r',encoding='utf8') as file:
groups = [group.strip() for group in file.readlines()] # to get names by avoiding blank space and newlines
with open('Whatsapp_Message.txt','r',encoding='utf8') as file:
message=file.read()
for i in groups:
# in search bar searching the group name
search_bar_xpath = '//*[@id="side"]/div[1]/div/div[2]/div[2]/div/div[1]/p'
search_bar=WebDriverWait(brw,500).until(EC.presence_of_element_located((By.XPATH,search_bar_xpath)))
pyperclip.copy(i) # pyperclip : used to allow copy programmatically
search_bar.send_keys(Keys.CONTROL+'v') # send_keys : used to paste by using keyboard actions "ctrl+v"
time.sleep(2)
#entering group name and clicking
group_title_xpath = f'//*[@title="{i}"]' # getting the xpath by title and changing it for every run by each group name
group_title=brw.find_element(By.XPATH,group_title_xpath)
group_title.click()
# inputting message in the text area
time.sleep(2)
message_bar_xpath = '//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[1]/p'
message_bar=brw.find_element(By.XPATH,message_bar_xpath)
pyperclip.copy(message)
message_bar.send_keys(Keys.CONTROL+'v')
message_bar.send_keys(Keys.ENTER)
time.sleep(3)
time.sleep(10)