DEV Community

loading...

How To Handle Chat Bot in Selenium WebDriver using Java

Raghwendra Sonu
Automation Testing expert with diverse tools and technologies. Expertise in Web Automation, Mobile Automation, Performance Testing, API (WebServices Testing), Automation Testing Frameworks.
・2 min read

A chatbot is an artificial intelligence (AI) software service that can simulate a conversation or chat. Natural Language Processing is used in this. Now a days they are frequently used in messaging applications, websites, mobile apps and on telephone.

Alt Text

The problem is if you don't handle them, sometimes, they may get overlapped with application pages, and that will not allow Selenium to perform any activity on web page causing, scripts may fail.
We will talk about approaches to deal with chat bot in Selenium.

Usually, chat bots gets popped up automatically on launching the application or website. Sometimes, it takes few seconds to get it on the page. So, the behavior will be :

  1. Website/ application page gets displayed
  2. Chat bot gets displayed. One thing to observe here is, usually chat bots has a close button. So, in Selenium how we are going to handle is as follows:
  3. Navigate to the website/ application.
  4. Explicitly Wait for 60 seconds to get Chat Bot displayed.
  5. Once it pops up, click on close button.

Here is a sample code in Selenium Java to handle Chat Bot displayed on Lazada(Shopping website).

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Homepage {
public static void main(String[] args) {
System.setProperty("webdriver.firefox.marionette","C:\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String baseUrl = "https://www.lazada.sg/helpcenter/?spm=a2o42.home.header.5.654346b5LPMwYS";
driver.get(baseUrl);
driver.findElement(By.xpath("//span[text()='Chat Now']")).click();
WebDriverWait wait = new WebDriverWait(driver,60) ;
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("alime-header-close-button")));
driver.findElement(By.id("alime-header-close-button")).click();
System.out.println("Found Closing button for Chat Bot, clicked on that...");
driver.close();
System.exit(0);
}
}

I hope this was helpful. If you have any queries or questions please let me know through comment section.

Discussion (1)

Collapse
nivya1391 profile image
nivya1391

Hi Raghwendra, I need a some help related to a chatbot ! So we have created a chatbot and we use it to test our test cases it takes one testcase at a time and after few mins displays the result . Now we have automated that with selenium could you please help me out as to how to read the response/result from the chatbot screen?

Forem Open with the Forem app