WhatsApp Send API or Click To Chat Automation using Selenium & C#
WhatsApp is known for its simple, secure and reliable messaging application with cross-platform support. The tremendous growth in the number of active users has brought many organizations and individuals to look out for an API to create bots in WhatsApp, but the sad news is WhatsApp does not entertain bots in its application and there is no official API available to directly integrate with users code.
WhatsApp has provided a feature or service called Click to Chat, which allows you to begin a chat with someone without having his or her phone number saved in your phone's address book. We can use this feature combined with WhatsApp Web and Selenium to create a script, which can automatically send messages to phone numbers.
Note: The contents and methods provided in this article are only for educational purpose. There is a high chance for WhatsApp to block the number permanently if you are violating any WhatsApp policies. Using this method is at your own risk
How to use WhatsApp Click to Chat
- Create a Click to Chat link using full phone number in international format and message that has to be send. Click here for examples
- Open the newly created WhatsApp Click to Chat URL in a web browser of your choice like Google Chrome, Firefox, and so on
- The browser will redirect you to https://api.whatsapp.com with a SEND button and the text provided while creating the link at the bottom of the page
- Click the “SEND” button
- WhatsApp Web will redirect you to login page if you not logged in, else you will be redirect to the messaging page. (Login to WhatsApp Web when prompted)
- In the chat area page you could see the phone number/person will be selected based on the number used while creating the Click to chat link and the message body is auto filled with the text used while creating the link.
- The final step is to click on the Send button
Why Selenium?
Selenium WebDrive API supports different programming languages like C#, Java, Perl, Python and so on. One can find the full list of supported programming languages and operating system in the link https://www.seleniumhq.org/about/platforms.jsp
WhatsApp Send API / Click to Chat automation using Selenium and C#
We can implement Click to chat feature-using Selenium and with any programming languages supported by Selenium. The code in this article explains Click to chat or Send API automation using Selenium with C#
using OpenQA.Selenium; using OpenQA.Selenium.Chrome; using System; using System.Diagnostics; namespace WhatsAppSendApiAutomation { class Program { IWebDriver driver; private bool CheckLoggedIn() { try { return driver.FindElement(By.ClassName("_2Uo0Z")).Displayed; } catch (Exception e) { Debug.Print(e.Message); return false; } } private void SendMessage(string number, string message) { driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10); //Wait for maximun of 10 seconds if any element is not found driver.Navigate().GoToUrl("https://api.whatsapp.com/send?phone=" + number + "&text=" + Uri.EscapeDataString(message)); driver.FindElement(By.Id("action-button")).Click(); // Click SEND Buton driver.FindElement(By.CssSelector("button._2lkdt>span")).Click();//Click SEND Arrow Button } public void run() { driver = new ChromeDriver(); driver.Navigate().GoToUrl("https://web.whatsapp.com"); while (true) { Console.WriteLine("Login to WhatsApp Web and Press Enter"); Console.ReadLine(); if (CheckLoggedIn()) break; } SendMessage("replace with your number", "replace with your text"); //*** Replace here ***// } static void Main(string[] args) { Console.WriteLine("Welcome to CraftedForEveryone.com"); new Program().run(); } } }
NuGet Packages Used
- Selenium.WebDriver
- Selenium.WebDriver.ChromeDrive
Code logic explained
- The very first set is to initialize the IWebDriver which is the core object of Selenium
- We are using Google Chrome browser in our project and hence we have injected ChromeDriver to the IWebDriver object by using driver = new ChromeDriver()
- If you wish to use Firefox as preferred browser then its corresponding Firefox package or executable file should be included in the project and the line new ChromeDriver() line has to be replaced with new FirefoxDriver()
- Once we have our browser driver ready we are going to login to WhatsApp Web, which has to be done manually. The line Navigate().GoToUrl () redirects you to the webpage where we need to login by scanning the QR Code
driver.Navigate().GoToUrl("https://web.whatsapp.com");
- The CheckLoggedIn() function runs in an infinite loop to check if we are logged in, since without logging in we won’t be able to perform any actions like sending messages
- The final step is to send the automated messages. Here we are using the function SendMessage() with phone number in international format and text message.
- The actions performed by SendMessage() function are
- Create the WhatsApp Click to chat/Send API link and open it in browser
- Click on the SEND Button
- Once the chat area is loaded, again click on the send button to deliver the message
Is there a way to send message to group ?, On python is possible, but i’m not sure in .net ?
Hi Victor,
Yes you can send messages to groups. To send messages to groups you cannot use the “Click to Chat” method.
“Click to Chat” method is especially for sending messages without saving the phone number in users contact list.
The whole concept is constructed upon “Selenium“. We can use Selenium to execute all the actions we execute manually in a browser in an automated manner.
Selenium is supported for Java, C#, Ruby, Python, JavaScript (NodeJs), Perl, PHP, and few other languages. In all these language, the above concept can be replicated.
To send message to group we usually execute/perform the following actions in WhatsApp Web
1. Login to WhatsApp Web
2. Search for the group name
3. Select the group name and enter the message to the send in chat area
4. Click on the send button
You need to write, code to replicate these 4 steps using Selenium in any programming language of your choice supported by Selenium.
Soon I will come up with articles and examples on Whats App Automation using Selenium and Python.
Hello, is there a way to close the session on web whatsapp. For example, when the program is closing.
What would the code look like?
Thank you!
Yes you can logout, WhatsApp web provides you option to logout. It will be two step action.
1. Find the menu button and click on it
2. Click on Logout Button
For both the actions its corresponding element has to be found using Selenium, either by CSS Selector or ID and click the button
I got the following exception
“_2Uo0Z” not found
Could you please elaborate your exception.
Element is _2rZZg instead in the latest webversion of WhatsApp
_2rZZg for this also it is throwing error…
what about webhooks, how can i get new messages?
This method does not support webhooks. But to have such kind of implementation you have to iterate your code and keep on wait for the incoming messages in the chat window.
can we send multi pal msg with this process i have tried but some errors are come when the second msg
please tell me how send second msg
Yes, you can send multiple messages, but you need to modify this code a little.
When you are trying to send the first message you would have already logged in to whats app and your message would be already filled in the text-box if you are using click to chat method.
In this case you will be just finding the send button using Selenium will click it.
When it comes to the second message you can avoid the login process of click to chat method and directly find the text box element with Selenium, fill your message and click on Send Button
Sir i used the Loop method to pass no. to url the no. is pass but at time of send
i just pass the link to with go url method
but second msg link was show but is send error when the next process
An unhandled exception of type ‘OpenQA.Selenium.NoSuchElementException’ occurred in WebDriver.dll
hello sir please upload one blog on multi pal message sender using the same code
Thanks you sir in advance i have learn lots from this
but second msg link was show but is send error when the next process
please check this error
An unhandled exception of type ‘OpenQA.Selenium.UnhandledAlertException’ occurred in WebDriver.dll
Additional information: unexpected alert open: {Alert text : }
(Session info: chrome=79.0.3945.117)
The problem is on a PC if you have the windows app installed, it wants to open that, no way to go around it ??
Hello, mine keeps asking login to whats app web and press enter, what could be the problem even after scanning it doesnt go through
Hi Dear,
Thanks for this post and very useful. I am near to the complete the solution, I am getting below code line.
driver.FindElement(By.Id(“action-button”)).Click(); // Click SEND Buton
driver.FindElement(By.CssSelector(“button._2lkdt>span”)).Click();//Click SEND Arrow Button
and its all time asking to open whatsapp web
Thanks in Advance.
Rohit
Hi sir i wnana be send a msg multiple person with selenium and C# through whatsapp url 1 msg send done but after then erreo and pop up a msg how can i handle
how send whatsapp button message using chromedriver in selenium vb 2019