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
- Right-click on project > new > Class
- Write class name (e.g. SendMsg).
- Click on finish
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);
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
}
}
catch (IOException e)
{
e.printStackTrace();
}
Reference : How to execute Selenium scripts on an already opened browser:- https://youtu.be/4F-laDV9Pl8
This is very helpful to send a bulk data with automation.
ReplyDelete