In the previous article we introduced the necessary tools in order to develop our Whatsapp Online Tracker.
Today we are going to see how to put all those ingredients together.
Whatsapp Online Tracker Course
You’ll learn to:
- Program in Python with a pratical approach.
- Use Selenium for automating web applications.
- Use XPath to select a specific node in a web page.
- Build your Telegram bot and program it by using Telepot library (send/receive messages and and pictures, filter users, react to specific commands, etc…).
- Write your own Python library and develop a complete Whatsapp API for your applications.
- Build step by step a Whatsapp Online Tracker application which can run on different systems.
- Send notification when somebody accesses Whatsapp.
- Install everything on a Raspberry Pi.
- …
The Solution
Let’s start showing the working code, we are going to spend a little bit of time in order to explain the most important blocks.
Remember that in order to get the job done, you need to have a phone number with an active Whatsapp account.
Selectors and Contact List
As we have seen in the first part of this guide, XPath selectors play an important role.
Thanks to this instrument we can uniquely identify all the elements in a web page.
In our case we need to select the following components:
With the first one we obtain a link to the new chat button.
The second one offers a reference to the text area that shows up when you click to the previous button and is used for inserting the phone number you want to control.
The third one allows to get the point in which the user connection state is shown.
At the end, we define a dictionary containing the users that we want to track. The list is composed by a nickname and the phone number.
The WebDriver
Now that we have defined the fundamental ingredients represented by the selectors and the contact list, it’s time to create an object which interacts with our browser.
With that, we can load the Whatsapp Web page and instantiate the wait object which allows to define some interesting functionalities:
In the previous code, we ask Selenium to wait for the new chat button and then we click on that button.
In order to simulate the keyboard input, we use the following instructions:
As you might have noticed, in this case we are asking Selenium to write the phone number of the contact we want to track in the text are that shows up after the previous click and, after 1 second, press ENTER.
At this point, we just have to wait for few seconds and then check that the online label is visible under the user’s name.
If so, we can infer that the contact is online, otherwise will be offline.
If you’re interested in extending the functionality of your tracker to check numbers not in your address book, check out this article.
Conclusions
Ok, we have seen which are the main ingredients that let our tool working.
Once terminated the code analysis, just start the script.
If everything is correct, we should see a browser window loading the Whatsapp Web page.
Once paired your device using the QrCode, the main Whatsapp Web interface will be loaded and then Selenium will start to programatically check the users specified.
Is there any risk of blocking the number? WhatsApp does not like its content being scratched
Hi Mauro,
Thanks for your comment!
Well, in my experience I’ve never heard of numbers being blocked, even because you are (legally) using a WhatsApp service (no spam, no modified app)…nothing else.
Why WhatsApp should block a number simply because it’s periodically opening one of his service? ;-)
At the end such a solution has nothing more than open WhatsApp web in your browser and let it run for days.
So, no worries!
Having issue with input_box.send_keys(TARGET[target]). This part is not running, it’s giving exception 2. ANy solution to this?
Hi,
Please, can you post the entire exception you get?
P.S.
In order to do that modify the line 66 this way:
except Exception as e:
And line 67 this way:
print(e)
This way we can know exactly why the script crashes.
How can it be deployed for example to a webserver or find a way so that it can run automatically online (useful when my pc is switched off) or maybe turn it into a telegram bot so that it can be run from pc or iphone or android with no difference.
Im sorry for so many question, hopefully you’ll help me!
Regards
Hi Steven,
Basically what you are asking for is whether it is possible to develop an interface in order to use the tracker even when you are not in front of your PC (or when you PC is switched off). This is possible in many different ways! Basically the script that I show you in these articles is just for showing the concept how the tracker should work… then develop that interface is something that depends on your needs. For example, as you have already thought, you can develop a telegram bot which answers to commands via app! Of course this bot has to run somewhere, in my case I developed a more structured system (based on this script) which runs on a raspberry pi! The raspberry pi allows me to have a low-cost PC that can stay switched on 24/7 with also low consumptions. In the raspberry pi I built a telegram bot which allows me to interact with the tracker itself.
If you want to see how I realized that you can join my course at this link. Inside you can find the complete code and also explanation on how I reached all the goals.
Obviously once you have such an interface, you can install it everywhere not only on a raspberry.
I don’t know if I answered to your questions, so please let me know! I’ll be happy to help you!
Macheronte
Hey Macheronte! Can you please tell me what will happen when we execute this code? sorry for this silly question.. ;)
Hi Nakul,
thanks for your comment.
This is a simple script that, according to the phone numbers you set in the TARGETS list, allows you to automatically check when the related users log in and when they log out.
In other words it’s a (very basic) tracker for checking if and when a contact is online during the day.
If you want to give a try, remember that the parameters that I show on the articles (i.e. xpath selectors) may have changed during these months, so I reccomend you to follow the 2 articles step by step, updating what is not up to date anymore.
Hope this can help,
Macheronte
Thank you for this.
I’m using firefox as my webdriver
browser = webdriver.Firefox()
Would that make a difference?
Also, when the code executes, it’s supposed to just sit there until one of the TARGETS come online, am I mistaken in my presumption?
Been waiting for a while with no errors and it does not show anything, so asking.
Again, thank you for this.
Pl. help!
Using
browser = webdriver.Firefox()
Putting the latest geckodriver at the right path/directory /usr/local/bin/
after around 5 minutes, I get the error below.
Getting the error as below:
Traceback (most recent call last):
File “./monitor+log.py”, line 41, in
new_chat_title = wait.until(EC.presence_of_element_located((By.XPATH, NEW_CHAT_BTN)))
File “/usr/local/lib/python3.7/dist-packages/selenium/webdriver/support/wait.py”, line 80, in until
raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:
Hi James,
thanks for your comment.
It seems to me that the NEW_CHAT_BTN xpath selector is not correct.
Did you copy the code in the article as it is?
Keep in mind that Whatsap Web changes its own xpath selectors… so it’s worth updating them as explained in this article.
Try to execute the steps explained in that article and update the selectors for the following variables:
NEW_CHAT_BTN
INPUT_TXT_BOX
ONLINE_STATUS_LABEL
Let me know for any further clarifications,
Macheronte
Am interested in a more improved version of the code
Can you do this as a freelance job for me?
please drop me an email
Hi Armando,
I’m going to send you a private email. Let me know with a comment in here if you don’t receive it in the next few minutes.
Thanks,
Macheronte
Anche io come Armando sarei interessato, puoi contattarmi su skype tramite email registrazione?
Ciao Michel,
certo! Ti contatto più tardi.
Buon proseguimento,
Macheronte
[11224:9516:0425/063205.444:ERROR:gpu_init.cc(426)] Passthrough is not supported, GL is disabled
Can you please help me with this error?
Hi,
Can you give me more details about how you get this error? Which OS do you have? What are you trying to do?
The more info you give me, the better I can understand what may be the cause!
Thanks,
Macheronte
I have the same error: [7012:11756:0501/152231.650:ERROR:gpu_init.cc(426)] Passthrough is not supported, GL is disabled] – when I execute the above WhatsApp Tracker.
It does open web.whatsapp.com, gives me the QR code, but then nothing more happens on the Chrome window and I get the above error
Running Chrome 90 on Windows 10
Hi Mokus4x,
thanks for you message.
Please, try to do the steps reported at this link and let me know if the problem is solved.
Thanks,
Macheronte
Hi Macheronte,
Thanks – I followed all the steps, but the problem persists. in chrome://gpu it does say
WebGL: Hardware accelerated
WebGL2: Hardware accelerated
So I dont know where this comes from. I also activated Override software rendering list, but no change…
Ciao Mokus4x,
Ok… can you tell me which version of chromedriver have you installed? Is the same as your Chrome version?
After the WhatsApp Web page is open and you can see the qrcode, did you try to pare your device by opening WhatsApp in your phone and selecting WhatsApp Web?
May you explain me what happens in details, please?
Thanks,
Macheronte
So great
some questions: 1) does it save logs in some were. if not how can we log it in somewhere
2)after coding we need to just run it ?? true ??
3) does it still working or not ??
really tnx for this article
Ciao Ali,
Thanks for your comment, I appreciate it.
Let’s answer to your questions:
1) No, this script is just for demonstration purpose. It doesn’t log anything. If you want to do it you have a lot of possibilities, the simplest one: you should search on the internet how to write data on a text file and print the timestamp of each connection event.
2) yes, you just need to run it.
3) I don’t know, because the xpath parameters I used could be changed in this last months, you should give it a try. For sure the concept is correct, in order to let it run is just a matter of a couple of simple adjustments. Follow the article and you’ll have a working script.
Instead if you want to have a program more powerful and complete, check my course in which I teach you how to write your Python tracker by yourself. Here the link.
Bye,
Macheronte
Hi, is there a way to do this whole task in a headless browser?
Hi Martin,
Unfortunately according to what I saw during my tests it’s not possible. You must have the browser GUI open to correctly get the different components of the WhatsApp Web interface.
Have a nice continuation,
Macheronte
Thank you for your extreme fast reply! Yeah I thought that there is no way to pass this with selenium.
You are welcome, Martin!
Feel free to comment when you want.
Have a nice evening,
Macheronte
Hii Macheronte…
I know my question will be a little silly, but can you please tell me that the course that you are providing in the Udemy, is it about making a tracker and storing the data somewhere and is it about making an android app? Cause I need to track someone’s online status in whatsapp, will I be able to do that after watching your course in Udemy?
Please reply cause I need to have t
Hi Saurabh,
thanks for your question!
The course I made is about writing a tracker from scratch that can work even in a Raspberry Pi (which has to be switched on 24/7) and can be controlled through the Telegram application installed in your phone. By using that app you can send command to your tracker and get notifications everytime the user you want to track, accesses its WhatsApp account. The code is explained step by step. Of course for any need you can send me questions and I’ll answer you asap. No android applications are developed, I based everything on Telegram in order to concentrate all the efforts only on developing the tracker.
If you are still undecided, you can give it a try and, if you are not satisfied, you can ask for a refund withing 30 days!
For any other need feel free to ask,
Macheronte
Hello sir , I am getting this error can you please solve it!!!
TimeoutException Traceback (most recent call last)
in
31
32 # Wait untill new chat button is visible
---> 33 new_chat_title = wait.until(EC.presence_of_element_located((By.XPATH, NEW_CHAT_BTN)))
34
35 while (tryAgain):
/opt/anaconda3/lib/python3.7/site-packages/selenium/webdriver/support/wait.py in until(self, method, message)
78 if time.time() > end_time:
79 break
---> 80 raise TimeoutException(message, screen, stacktrace)
81
82 def until_not(self, method, message=''):
TimeoutException: Message:
After running this code, I have successfully opened up WhatsApp web and then I don’t know where to add the phone numbers. Also where will I receive the notifications!
Hi Harsh,
well, your problem might be caused by 2 factors:
1) when the WhatsApp web page appears, it shows a Qr code for pairing your device with the WhatsApp Web session (check this link). Isn’t it? If so, you have to use a smartphone to start the session. This step is necessary in order to load the WhatsApp dashboard where the script will start to gather information about the contacts you want to track.
2) if you’ve already paired the device, and the script correctly opens the WhatsApp web dashboard, the problem should be on the
NEW_CHAT_BTN
XPath that has to be updated. Infact, if the Xpath is not correct, Selenium won’t find the button for starting a new chat and, after a period of time, it will throw a timeout exception. In order to update this path, check this article and this and repet those steps for the NEW CHAT button shown in the WhatsApp dashboard.in both cases, the phone numbers have to be added into the script (line 15).
Unfortunately this script only shows the events of connection of a specific user by printing a string into the console. The article is for demonstration purpose only. If you want to build an entire application and get the notifications on Telegram as a message, you can give a try to my Udemy course. In this course I guide you step by step on writing a complete WhatsApp Online Tracker application that can send notifications on Telegram app.
Hope this can help,
Macheronte
Thanks a lot sir for your reply but I am still confused how I am able to change the xpath. This is shown in my xpath extension.
Rel XPath //body
1 cssSelector body
1 Abs XPath /html[1]/body[1]
1 tagName body
And after opening the WhatsApp web what we have to do can you please tell, I am sorry for my questions!!
Hi Harsh,
I’m sorry but I don’t understand where you took those lines you wrote.
Did you follow all the steps explained in the 2 articles?
Can you tell me which of those steps is not clear or doesn’t work as expected, please?
This way we are both sure to be on the same page and I can help you more efficiently.
Thanks,
Macheronte
Alright I will explain it again
After running my code successfully WhatsApp web is launched and I scanned the qr code through my phone. Also I added the phone numbers on line 15 whom I want to track. So where will I receive the notifications of online and offline. And the code I mentioned it above was of chropath. So can you please tell me where I will be receiving the notifications and have I to keep my WhatsApp web open all the time to receive notifications!!
Dear Harsh,
here the answers you need:
1. The XPath values you get from ChroPath, have to be written in the lines 10, 11 and 12 of the script. This paths are necessary in order to help Selenium recognizing the different part of the WhatsApp Web dashboard. The values already written should work, but you must consider that sometimes WhatsApp changes those values, so if the script is not working you have to follow the steps explained in this article and apply them in the WhatsApp Web dashboard in order get the updated xpath values.
If you didn’t understand what
NEW_CHAT_BTN
,INPUT_TXT_BOX
andONLINE_STATUS_LABLE
are about, please have a look on this article in which I explain with pictures where are placed these components in the WhatsApp Web dashboard interface.2. As already written in my previous answer, this script is for demonstration purpose only. Once the contact you are trying to track goes online, the script will print a simple string into the console you’ve used for launching the application (lines 57-61).
3. Yes, you must keep your WhatsApp Web page open all the time, this is a limit of Selenium that in this case can’t work in headless mode.
Have a nice continuation,
Macheronte
Forgive me if it is not a good question. I just wonder one thing about this stalker. To use this we must log in and open the WhatsApp web, am I right? What if the user that we stalk just sends a message to us while we are stalking. Aren’t we going to see it directly? Does this project works in the background or should it be work while that user’s chat screen is open on the computer?
Ciao Jack,
And thanks for your message.
What you said is not completely wrong, it all depends on which number you use for tracking a contact.
If I had to control somebody I definitely would use a number that the victim doesn’t know.
Since the contact doesn’t have the number I use, it’s impossible for him to send me a message and to find out that I have an open chat with his number (that, in order to answer to your last question, it’s a mandatory requirement in this solution).
While we are at that, if you want to see also how to send a message via WhatsApp Web to a number which is not on your contact list have a look on this article! Mixing the 2 solutions, this one plus the one of the link above, will produce improved versions of the tracker.
Feel free to ask for other doubts!
Have a nice continuation,
Macheronte
Hello Macheronte and thank you for the efforts. I have a question if I may : is there any way we can track multiple contacts? How can we approach that problem? Thank you!
Hi Eddy,
thanks for your message!
Tracking multiple contacts is possible but, due to the project’s nature, we have some limitations to take in consideration.
What you can do is to let the script iteratively check one user at a time. To be more specific:
1) Open the chat with the user1.
2) Wait t seconds to load the status bar and get the current user1 status.
3) Open the chat with the user2.
4) Wait t seconds to load the status bar and get the current user2 status.
…
Of course this method can work if you have few users to track. If you have a big amount of different targets, it cannot work well because it’ll collect each status every t * number_of_users seconds. If number_of_users is too big, the resulting sampling time is really slow.
At that point, the only possibility is to use several phone numbers in order to open multiple WhatsApp Web interfaces. If you consider 5 trackable users per interface, with 5 WhatsApp Web accounts you can reach 25 potential users.
Hope this can help,
Macheronte
can you pls show how to add time instance to code as well so that we can also get at what time the target was online .?
Hi Heena,
sure, here a possible modification to the last code shown in the article:
import datetime
try:
browser.find_element_by_xpath(ONLINE_STATUS_LABEL)
print(str(datetime.datetime.now()) + ' ' + target + ' is online')
except:
print(str(datetime.datetime.now()) + ' ' + target + ' is offline')
Hope this can help,
Macheronte