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...

Playwright frameLocator() Guide (2026): Automate Frames & iFrames Like a Pro

Playwright Frames & iFrames Explained: frameLocator(), Nested Frames & Best Practices (2026) Reading Time: 20–25 Minutes Level: Beginner to Advanced Category: Playwright Tutorial If you've ever written a Playwright test that couldn't find a button even though it was clearly visible on the page, there's a good chance you were dealing with an iframe . Frames are one of the most misunderstood topics in browser automation. Many beginners spend hours debugging perfectly valid locators before realizing the element is actually inside another browsing context. Modern web applications heavily rely on iframes for payment gateways, customer support widgets, embedded dashboards, videos, advertisements, maps, authentication pages, and third-party integrations. Fortunately, Playwright makes working with frames much easier than traditional automation frameworks through its powerful frameLocator() API. In this complete guide, you'll learn how frames w...