0
 String[] arr = {"Month","Jan","Feb","Mar", "Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
 WebElement dropDown = getDriver().findElement(By.id("month"));
 Select select = new Select(dropDown);
 List<WebElement> options = select.getOptions();

 for(WebElement we : options){
     for(int i = 1; i<arr.length; i++){
         if(we.getText().equals(arr[i])){
             System.out.println("Matched" );
             break;
         }
     }
 }

I want to find duplicate months on a Facebook dropdown by using Selenium in Java.

I tried findelement, web element but I could not do that. Right now; I can match the months with the webpage. Original question is like this: find if month dropdown contains any duplicates. If yes then print out those values which are duplicate. Thnx

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Bob Lerry
  • 15
  • 5
  • 2
    Hi, welcome to StackOverflow. Please follow the guidelines given on https://stackoverflow.com/help/how-to-ask, provide enough information and the code which you have tried so far. – Kamal Jan 30 '19 at 01:36
  • 1
    Why would there be duplicate months in a dropdown? – orde Jan 30 '19 at 03:32
  • You are right. Maybe I could not explain exactly. My professor asked me this question. Original question is like this: find if month dropdown contains any duplicates.if yes then print out those values which are duplicate. My case is Facebook dropdown.Thnx – Bob Lerry Jan 30 '19 at 06:06
  • Possible duplicate of [Identify duplicates in a List](https://stackoverflow.com/questions/7414667/identify-duplicates-in-a-list) – JeffC Jan 31 '19 at 14:55
  • @MarkRotteveel Can you please help me to understand why this question qualified as **too broad**? OP clearly mentioned s/he _want to find duplicate months_ which Facebook dropdown doesn't have and flatly accepted and conveyed _You are right. Maybe I could not explain exactly. My professor asked me this question_ along with his code trials. Doesn't it qualifies as a legitimate question as a _New contributor_? – undetected Selenium Jan 31 '19 at 16:41

1 Answers1

0

As your usecase is to find duplicate months on the Facebook dropdown by using Selenium and Java you really don't need the reference array. To find if the month dropdown contains any duplicates and print out those values which are duplicate you can use the following solution:

  • This code block extracts the list of months from the dropdown on Facebook:

    System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.facebook.com");
    WebElement dropDown = driver.findElement(By.id("month"));
    Select select = new Select(dropDown);
    List<WebElement> options = select.getOptions();
    ArrayList<String> months = new  ArrayList<>();
    for(WebElement we : options)
        months.add(we.getText());
    System.out.println("Current list of months: " +months);
    
  • For the sake of demonstaration through this code block we will add 3 duplicated entries:

    months.add("Month");
    months.add("Feb");
    months.add("Dec");
    
  • In this code block we have created a function to return the duplicated months:

    public static Set<String> findDuplicates(List<String> listContainingDuplicates)
    { 
        final Set<String> setToReturn = new HashSet<>();
        final Set<String> set1 = new HashSet<>();
        for (String myString : listContainingDuplicates)
        {
            if (!set1.add(myString))
                setToReturn.add(myString);
        }
        System.out.println("Unique months: " +set1);
        return setToReturn;
    }
    
  • Now, you can call the function findDuplicates() as follows:

    System.out.println("Duplicated months: " +findDuplicates(months));
    
  • So the complete solution will be:

    package Duplicates;
    
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Set;
    
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.support.ui.Select;
    
    
    public class Find_Duplicates_In_List {
    
        public static void main(String[] args) {
    
            System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
            WebDriver driver = new ChromeDriver();
            driver.get("https://www.facebook.com");
            WebElement dropDown = driver.findElement(By.id("month"));
            Select select = new Select(dropDown);
            List<WebElement> options = select.getOptions();
            ArrayList<String> months = new  ArrayList<>();
            for(WebElement we : options)
            months.add(we.getText());
            System.out.println("Current list of months: " +months);
            months.add("Month");
            months.add("Feb");
            months.add("Dec");
            System.out.println("Duplicated months: " +findDuplicates(months));
        }
    
        public static Set<String> findDuplicates(List<String> listContainingDuplicates)
        { 
          final Set<String> setToReturn = new HashSet<>();
          final Set<String> set1 = new HashSet<>();
          for (String myString : listContainingDuplicates)
          {
           if (!set1.add(myString))
            setToReturn.add(myString);
          }
          System.out.println("Unique months: " +set1);
          return setToReturn;
        }
    }
    
  • Console Output:

    Current list of months: [Month, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec]
    Unique months: [Jul, Oct, Feb, Apr, Jun, Dec, May, Month, Sept, Aug, Nov, Jan, Mar]
    Duplicated months: [Month, Feb, Dec]
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352