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 in Selenium by Dhruv
1,079 views

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.get("http://www.flipkart.com/");
  driver.manage().window().maximize();
  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"));
  mobiles.click();//clicking on mobiles
 
  List<WebElement> brands = driver.findElements(By.xpath("//div[@id='list-tagcloud']/div[2]/a"));
  System.out.println(brands.size());
  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++){
   System.out.println(allLinks.get(i));
   driver.get(allLinks.get(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
   System.out.println(retails.size());
   for(int j=0;j<retails.size();j++)
   {
   
    System.out.println(retails.get(j).getText());//printing all the sellers info
   
   }
  
  }
 
  driver.close();
}}
answered May 21 by Anvi
• 13,960 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
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,110 points
1,176 views
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,150 points
1,261 views
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,150 points
1,018 views
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 in Selenium by Anvi
• 13,960 points
367 views