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

Post a Comment

Popular posts from this blog

Selenium 5: What’s New and Why It Still Matters in 2025

Selenium 5: What’s New and Why It Still Matters in 2025 data-full-width-responsive="true"> Selenium has been the backbone of web automation testing for over a decade. From the early days of Selenium RC to WebDriver and the release of Selenium 4, it has enabled QA engineers worldwide to automate browsers reliably. But as modern frameworks like Playwright and Cypress gained attention, critics started asking: “Is Selenium dead?” In 2025, the answer is clear: Selenium is not dead — it has evolved. With the release of Selenium 5 , the project has modernized to support new browser technologies, improve stability, and remain a cornerstone of test automation strategies. 1. Introduction — Selenium’s Legacy Selenium started in 2004 as a tool to automate browsers for functional testing. Over the years: Selenium RC gave way to Selenium WebDriver. Selenium Grid enabled parallel execution at scale. Selenium 4 introduced W3C WebDriver com...

Google Anti-Gravity Thinking in Software Testing (With Real-World Examples & Tools)

Google Anti-Gravity Thinking in Software Testing A practical mindset that prepares testers to break systems the right way Software testing is often taught as a structured activity. Write test cases. Follow steps. Verify expected results. Mark Pass or Fail. This works well in training environments — but real users don’t behave this way. They don’t read requirements. They don’t follow flows. They don’t wait patiently. They click early. They click repeatedly. They lose network. They rotate screens. They refresh pages. And when this happens, many applications fail silently. That is why production bugs exist. To catch these bugs early, testers must think differently. They must think beyond rules. They must think beyond assumptions. This is where Anti-Gravity Thinking becomes powerful. What Is Anti-Gravity Thinking in Testing? Google Anti-Gravity is a visual experiment where UI elements do not stay fixed. They float. They move. They fall out of place. In...

Chaos Testing for Automation Engineers

Chaos Testing for Automation Engineers Why automation passes in CI but fails in production ⏱ Reading time: 10–12 minutes Most automation engineers have experienced this moment: All test cases are green. Pipelines are passing. Confidence is high. And then production fails. This blog explains why that happens — and how Chaos Testing , inspired by Anti-Gravity thinking, helps automation engineers test reality instead of assumptions. Why Automation Testing Often Gives False Confidence Automation scripts usually validate: Stable environments Correct inputs Predictable flows Fast responses But real systems don’t behave this way. Production systems face: Network delays Service timeouts Partial failures Unexpected user behavior Chaos Testing exists to simulate these conditions intentionally — before users experience them. What Is Chaos Testing (In Simple Terms) Chaos Testing is n...