Skip to main content

WhatsApp Message using Selenium Java


Prerequisites :

  • Create a new folder on any driver except C Drive. The folder name should be “Chrome_Test_Profile”.
  • Set the chrome application path to an environment variable. Follow the below steps to set environment path in windows: 
  • In Search, search this: System (Control Panel) 
  • Click the Advanced system settings link. 
  • Click Environment Variables. In the section System Variables, find the PATH environment variable and select it. Click Edit. If the PATH environment variable does not exist, click New. 
  • In the Edit System Variable (or New System Variable) window, append the path. (E.g. C:\Program Files (x86)\Google\Chrome\Application). 


Once you are done with all the above, setup follow the below steps:

1. Open Chrome In debug mode using the following command.
Command:
chrome.exe -remote-debugging-port=9014 --user-data-dir="E:\Selenium Scripts\Chrome_Test_Profile” (This command opens the window in debug mode).



You can use any port, here I am using 9014.
By executing the above command it will create a new profile for the chrome. It will not affect the original profile of chrome.

2. Now open https://web.whatsapp.com and login with your account in chrome. Which is already running in debug mode.



3. Now Open Eclipse and create a new java project.
  • File > New > Java Project
  • Write Project Name (e.g. WhatsappMsgSender) and click on next.
  • Now go to library > Add external Jars ( Add All selenium Required jar files )Selenium standalone server and other Required jar files you can download from this link: https://www.seleniumhq.org/download/
  • Click on finish.

4. Now create a new package as below :
  • Right-click on project > new > package
  • Write a package name (e.g. whatsappmsg). 
  • Click on finish 
5. Now create a new class in the above-created package.
  • Right-click on project > new > Class
  • Write class name (e.g. SendMsg).
  • Click on finish
6. Create one main method in the above-created class and add following code for executing script in chrome which is already open in debugging mode in step 2.
System.setProperty("webdriver.chrome.driver", "E:\\Selenium Scripts\\libs\\chromedriver.exe"); 
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress","localhost:9014");
WebDriver driver= new ChromeDriver(options);
driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);


Above code will interact with the already open chrome browser.

7. Write Following code for clicking on new message
driver.findElement(By.cssSelector("#side > header > div.sbcXq > div > span > div:nth-child(2)")).click();
Thread.sleep(200);

8. To read contact number and message, Create a comma Separated CSV File in below format.
CSV Format:

Name 1, Message 1,
Name 2, Message 2




10. Write the following code to read a contact number and message from a CSV file.
try (BufferedReader br = new BufferedReader(new FileReader(csvFile)))
{

                 while ((line = br.readLine()) != null)
                       {
                           driver.findElement(By.cssSelector("#side > header > div.sbcXq > div > span > div:nth-child(2)")).click();
                            ContactInfo = line.split(cvsSplitBy);
                             int i = 0;
                             int j = 1;
                          driver.findElement(By.cssSelector("#app > div > div > div._37f_5 >                    div._3HZor._3kF8H > span > div > span > div > div:nth-child(2) > div > label > input")).sendKeys(ContactInfo[i]);
                       Thread.sleep(2000);
                       Actions action = new Actions(driver);
                       action.sendKeys(Keys.ENTER).build().perform();
                      driver.findElement(By.cssSelector("#main > footer > div._2i7Ej.copyable-area > div._13mgZ > div > div._3u328.copyable-text.selectable-text")).sendKeys(ContactInfo[j]);
                      Thread.sleep(2000);
                      action.sendKeys(Keys.ENTER).build().perform();
                     Thread.sleep(2000); 
                      i++;
                      j++;
                 }
}
catch (IOException e)
{
       e.printStackTrace();
}















Reference : How to execute Selenium scripts on an already opened browser:- https://youtu.be/4F-laDV9Pl8









Comments

  1. This is very helpful to send a bulk data with automation.

    ReplyDelete

Post a Comment

Popular posts from this blog

AI Agents in DevOps: Automating CI/CD Pipelines for Smarter Software Delivery

AI Agents in DevOps: Automating CI/CD Pipelines for Smarter Software Delivery Bugged But Happy · September 8, 2025 · ~10 min read Not long ago, release weekends were a rite of passage: long nights, pizza, and the constant fear that something in production would break. Agile and DevOps changed that. We ship more often, but the pipeline still trips on familiar things — slow reviews, costly regression tests, noisy alerts. That’s why teams are trying something new: AI agents that don’t just run scripts, but reason about them. In this post I’ll walk through what AI agents mean for CI/CD, where they actually add value, the tools and vendors shipping these capabilities today, and the practical risks teams need to consider. No hype—just what I’ve seen work in the field and references you can check out. What ...

Autonomous Testing with AI Agents: Faster Releases & Self-Healing Tests (2025)

Autonomous Testing with AI Agents: How Testing Is Changing in 2025 From self-healing scripts to agents that create, run and log tests — a practical look at autonomous testing. I still remember those late release nights — QA running regression suites until the small hours, Jira tickets piling up, and deployment windows slipping. Testing used to be the slowest gear in the machine. In 2025, AI agents are taking on the repetitive parts: generating tests, running them, self-healing broken scripts, and surfacing real problems for humans to solve. Quick summary: Autonomous testing = AI agents that generate, run, analyze and maintain tests. Big wins: coverage and speed. Big caveats: governance and human oversight. What is Autonomous Testing? Traditional automation (Selenium, C...

What is Hyperautomation? Complete Guide with Examples, Benefits & Challenges (2025)

What is Hyperautomation?Why Everyone is Talking About It in 2025 Introduction When I first heard about hyperautomation , I honestly thought it was just RPA with a fancier name . Another buzzword to confuse IT managers and impress consultants. But after digging into Gartner, Deloitte, and case studies from banks and manufacturers, I realized this one has real weight. Gartner lists hyperautomation as a top 5 CIO priority in 2025 . Deloitte says 67% of organizations increased hyperautomation spending in 2024 . The global market is projected to grow from $12.5B in 2024 to $60B by 2034 . What is Hyperautomation? RPA = one robot doing repetitive copy-paste jobs. Hyperautomation = an entire digital workforce that uses RPA + AI + orchestration + analytics + process mining to automate end-to-end workflows . Formula: Hyperautomation = RPA + AI + ML + Or...