-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.py
More file actions
51 lines (40 loc) · 1.86 KB
/
source.py
File metadata and controls
51 lines (40 loc) · 1.86 KB
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
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains
def sendTheMessage(wait, sendingText):
message_box_path = '//*[@id="main"]/footer/div[1]/div/span[2]/div/div[2]/div[1]/div/div[2]'
message_box = wait.until(EC.element_to_be_clickable((By.XPATH, message_box_path)))
ActionChains(driver).send_keys(sendingText).send_keys(Keys.RETURN).perform()
def clickTheTarget(wait, messages):
target = 'Shreyas IND'
contact_path = f'//span[contains(@title,"{target}")]'
contact = wait.until(EC.element_to_be_clickable((By.XPATH, contact_path)))
contact.click()
for sendingText in messages:
sendTheMessage(wait, sendingText)
options = webdriver.ChromeOptions()
options.add_argument("user-data-dir=C:/Users/shrey/AppData/Local/Google/Chrome/User Data")
driver = webdriver.Chrome(options=options)
driver.get("https://web.whatsapp.com/")
wait = WebDriverWait(driver, 100)
target = 'Deep learning '
contact_path = f'//span[contains(@title,"{target}")]'
contact = wait.until(EC.element_to_be_clickable((By.XPATH, contact_path)))
contact.click()
num_messages_to_retrieve = 10
for _ in range(num_messages_to_retrieve // 10):
driver.execute_script("window.scrollBy(0, -1000);")
time.sleep(2)
messages = []
message_elements = driver.find_elements(By.XPATH, '//div[@class="copyable-text"][@data-pre-plain-text]')
for message_element in message_elements[-num_messages_to_retrieve:]:
message = message_element.text
messages.append(message)
clickTheTarget(wait, messages)
while True:
pass