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
19,188 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("https://www.edureka.co/");
  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, 2019 by Anvi
• 14,150 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 https://www.edureka.co/community/47160/automate-purchase-adding-book-cart-flipkart-using-selenium? It deals with a similar ...READ MORE

answered Jan 13, 2020 in Selenium by Karan
• 19,610 points
8,340 views
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 22, 2019 in Selenium by Abha
• 28,140 points
6,310 views
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
• 28,140 points
11,697 views
0 votes
2 answers

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,500 points
13,673 views
0 votes
2 answers

Problem while using InternetExplorerDriver in Selenium WebDriver

enable trusted connection  in internet explorer by ...READ MORE

answered Aug 31, 2020 in Selenium by Sri
• 3,190 points
9,142 views
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,910 points
7,221 views
0 votes
2 answers

How to use such xpath to find web elements

xpath are two types. 1) Absolute XPath:    /html/b ...READ MORE

answered Sep 3, 2020 in Selenium by Sri
• 3,190 points
7,874 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, 2019 in Selenium by Anvi
• 14,150 points
6,785 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP