Automate the searching of Samsung brand mobile phones in Flipkart website using Selenium Webdriver.

0 votes
Automate the searching of Samsung brand mobile phones in Flipkart website using Selenium Webdriver.
May 20, 2019 in Selenium by Dhruv

1 answer to this question.

0 votes

Hey Dhruv, you can write automation script for searching Samsung phones on Flipkart webpage using Selenium Webdriver:

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;

public class SearchFlipkart{

public static void main(String[] args) {
  WebDriver driver = new FirefoxDriver();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
  Actions act = new Actions(driver);
  WebElement mainmenu= driver.findElement(By.xpath("//*[@id='fk-mainhead-id']/div[2]/div/div[1]/ul/li[1]/a"));
  act.moveToElement(mainmenu).build().perform();//taking mouse
  WebElement mobiles = driver.findElement(By.xpath("//*[@id='menu-electronics-tab-0-content']/ul[1]/li[1]/a"));;//clicking on mobiles
  List<WebElement> brands = driver.findElements(By.xpath("//div[@id='list-tagcloud']/div[2]/a"));
  for(int i=0;i<brands.size();i++){
   System.out.println(brands.get(i).getText());//fetching all the brands
   if(brands.get(i).getText().equals("Samsung"))//matching for samsung only
    brands.get(i).click();//clicking pon samsung
   brands = driver.findElements(By.xpath("//div[@id='list-tagcloud']/div[2]/a"));//for cache
  WebElement searchbox = driver.findElement(By.xpath("//*[@id='searchbox']/li[2]/form/input[4]"));
  searchbox.sendKeys("grand2");//in search box search for grand2
  driver.findElement(By.xpath("//*[@id='searchbox']/li[2]/form/input[5]")).click();//clicking on search icon
  List<String> allLinks = new ArrayList<String>();
  List<WebElement> data = driver.findElements(By.xpath("//div[@id='products']/div/div/div/div[2]/div[@class='pu-title fk-font-13']/a"));
  System.out.println("total fonds are =="+data.size());//total searches coming on the page
  for(int i=0;i<data.size();i++){
   System.out.println(data.get(i).getText());//printing all samsung phones
   if(data.get(i).getText().contains("Grand 2"))//search for grnad2 only
    String link = data.get(i).getAttribute("href");//fetching all the links
    allLinks.add(link);//adding them to list
   data = driver.findElements(By.xpath("//div[@id='products']/div/div/div/div[2]/div[@class='pu-title fk-font-13']/a"));// for cache
  System.out.println(allLinks.size());//total grand2 phones
  for(int i=0;i<allLinks.size();i++){
   driver.findElement(By.xpath("//*[@id='mplistings']/div/div/div[1]/div[3]/a")).click();//click on view sellers
   List<WebElement> retails = driver.findElements(By.xpath("//div[@class='cart-table']/div[starts-with(@class,'line seller-item')]/div[4]/a"));//fetching info
   for(int j=0;j<retails.size();j++)
    System.out.println(retails.get(j).getText());//printing all the sellers info
answered May 21, 2019 by Anvi
• 14,050 points

Related Questions In Selenium

+1 vote
1 answer

How can I automate the process of adding iPhone to cart in Flipkart using Selenium(java),Page Object Model and TestNG? Also validate if product is added and available in cart?

Hey check this It deals with a similar ...READ MORE

answered Jan 13 in Selenium by Karan
• 10,310 points
0 votes
1 answer

Automate a purchase by adding a book to the cart in flipkart using Selenium

Hello Junaid, this code snippet would help you ...READ MORE

answered May 21, 2019 in Selenium by Abha
• 27,910 points
0 votes
1 answer

What are the drawbacks of using thread.sleep() command in Selenium Webdriver?

Hey Eric, some of the drawbacks of ...READ MORE

answered Jul 5, 2019 in Selenium by Abha
• 27,910 points
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,510 points
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,890 points
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,890 points
0 votes
1 answer

How can I clear the text in a text box using Selenium WebDriver?

Hello Akriti, you can clear the text ...READ MORE

answered May 29, 2019 in Selenium by Anvi
• 14,050 points