'Detect if a WhatsApp message is sent using Selenium Webdriver
I am trying to making a program that sends a message on WhatsApp if someone else sends a message, but I don't know how to check when a WhatsApp message is sent.
I don't need to read the contents of the message, but it would be a bonus
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
# messages to send
messages = ["Message 1", "Message 2"]
options = Options()
options.add_argument("--user-data-dir=chrome-data")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.binary_location = "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe"
driver = webdriver.Chrome(r'C:\ProgramData\chocolatey\lib\chromedriver\tools\chromedriver.exe', options=options)
driver.maximize_window()
driver.get('https://web.whatsapp.com') # must be pre-authenticated
time.sleep(20)
#Recepient Name
driver.find_element_by_xpath("//*[@title='example]").click()
for message in messages:
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[2]/div/div[2]').send_keys(message)
driver.find_element_by_xpath('//*[@id="main"]/footer/div[1]/div[3]/button/span').click()
time.sleep(5)
Any help would be appreciated
Solution 1:[1]
When you send a message there is a small icon on the right bottom side of the message, you can get this element and check if the accessible_name
property is "Sent"
, "Read"
or "Delivered"
.
Example below: Icon you need to locate
if ".m4v" in file_path or ".mp4" in file_path or ".3gpp" in file_path or ".mov" in file_path:
is_sent = self.wait_fast.until(ec.visibility_of_element_located((By.CSS_SELECTOR, "#main > div._2gzeB > div > div._33LGR > div._3K4-L > div._2wUmf._1q25n.message-out.focusable-list-item > div > div.Nm1g1._22AX6 > div.copyable-text > div > div._19jKW > div > div > span")))
else:
is_sent = self.wait_fast.until(ec.visibility_of_element_located((By.CSS_SELECTOR, "#main > div._2gzeB > div > div._33LGR > div._3K4-L > div._2wUmf._1q25n.message-out.focusable-list-item > div > div.Nm1g1._22AX6 > div.cm280p3y.f4q7vbcz.ocd2b0bc.folpon7g.aa0kojfi.snweb893.g0rxnol2.jnl3jror.copyable-text > div > div.lrw9n60e.lhggkp7q.fz4q5utg.b9fczbqn > div > div > span")))
I'm using CSS_SELECTOR
and the selector is different depending on the kind of message, if it's a text, image or video.
After you locate it you can check if the accessible_name
property is "Sent"
, "Read"
or "Delivered"
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sylvester Kruin |