I'm trying to use "whatsapp-web", "selenium" and "python 3" to know whenever a whatsapp user comes online or go offline.
to explain more , this is how i want the script to work :
the script will be listening for a span (with title=online) to be displayed , when the span is displayed (it means the user comes online) i want the time at this moment to be printed, then the script will keep listening again for the span to disappear, when it disappears the script print the time of disappearance, and so on.
this is my code :
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import datetime
driver = webdriver.Chrome('C:/webdrivers/chromedriver.exe')
driver.get('https://web.whatsapp.com/')
# do nothing until QR code scanned and whatsapp-web is accessed
input('Enter anything after scanning QR code')
# Input the name of the user to track
name = input('Enter the name of the user : ')
# find the whatsapp user to be tracked then a click to enter the conversation
user = driver.find_element_by_xpath("//span[@title = '{}']".format(name))
user.click()
while True:
# in the conversation page, a span with title online is diplayed when user is online.
#the web driver will wait 8hrs=28800s, if user not online all this time script will be killed by webdriverWait
element = WebDriverWait(driver, 28800).until(
EC.visibility_of_element_located(
(By.XPATH, "//span[@title = 'online']")))
#Moment the user came online
now = datetime.datetime.now()
print("online at : ")
print(now.strftime("%H:%M:%S"))
element = WebDriverWait(driver, 28800).until(
EC.invisibility_of_element_located(
(By.XPATH, "//span[@title = 'online']")))
#Moment the user went offline
now = datetime.datetime.now()
print("offline at : ")
print(now.strftime("%H:%M:%S"))
print("************")
My script works but, I want it to be running for hours , like 8 hour or maybe more, but i read that it is a bad practice to use WebDriverWait with a high number of seconds (28800s in my case).
So is there any other better way to achieve this ?
how can i write my output to a txt or word file?
Any suggestion to make my code better ?
How to prevent CPU slamming ? or any possible problem that may happen