1

I have this select with months and years, I need to sear every month of 2016 one by one and click the "query" button wait load the data on the page and pick up some data, then move on to the next month and repeat the process continuously, Until the end of 2016.

I thought about doing this using a list and an arraylist, but I would have performance issues.

What you suggest in this case,An Iterator <WebElement>?

How do I select the months and year 2016 one by one?

List<WebElement> meses = driver.findElements(By.id("sel_Mes"));

        Iterator<WebElement> itr = meses.iterator();
        while(itr.hasNext()) {
            System.out.println(itr.next().getAttribute("value"));

        }

or

List<String> mes = new ArrayList<String>();
        mes.add("Janeiro");
        mes.add("Fevereiro");
        mes.add("Março");
        mes.add("Abril");
        mes.add("Maio");
        mes.add("Junho");
        mes.add("Julho");
        mes.add("Agosto");
        mes.add("Setembro");
        mes.add("Outubro");
        mes.add("Novembro");
        mes.add("Dezembro");

        List<String> ano = new ArrayList<String>();
        ano.add("2016");

daTE

Paulo Roberto
  • 1,498
  • 5
  • 19
  • 42
  • Well, does it work ? The first solution looks good (generic and concise). Just try it ! – Tim Apr 05 '17 at 13:32
  • @Tim Yes, but as I am an apprentice in programming, I can not develop the logic part so that it effectively seals the month January and year 2016, how would I do it and after that, how would I continue the selection of the other months until arriving in December? – Paulo Roberto Apr 05 '17 at 13:37
  • Oh I see. You will need a nested loop: A loop in a loop. The outer loop could loop over all the years, and the inner loop could loop over all months. So if your first example works, just do the same for the years and get an iterator for the select. – Tim Apr 05 '17 at 13:42
  • @Tim Would you mind posting an example of how this code would look, please? – Paulo Roberto Apr 05 '17 at 13:46
  • I have posted an answer (not tested !). Hope this helps – Tim Apr 05 '17 at 13:48

1 Answers1

1

Try something like this:

WebElement selectElementMes = driver.findElement(By.id("sel_Mes");
Select select_mes = new Select(selectElementMes);
List<WebElement> allMes = select_mes .getOptions();

WebElement selectElementAnos = driver.findElement(By.id("sel_Ano");
Select select_anos = new Select(selectElementAnos);
List<WebElement> allAnos = select_anos .getOptions();


Iterator<WebElement> itr_meses = allMes.iterator();
Iterator<WebElement> itr_ano = allAnos.iterator();

while(itr_ano.hasNext()) {
    while(itr_meses.hasNext()) {
        System.out.println(itr_ano.next().getAttribute("value") + " -> " + itr_meses.next().getAttribute("value"));
    }
}

This should print something like:

2016 -> Janeiro

2016 -> Fevereiro

etc.

Tim
  • 3,910
  • 8
  • 45
  • 80
  • Excellent, in this case he is printing only the memos that are already selected by default, 04/2017 (month and current year), how do I print them every month in the sequence? – Paulo Roberto Apr 05 '17 at 14:00
  • Look at the third answer here: http://stackoverflow.com/questions/16768318/how-can-i-get-all-elements-from-drop-down-list – Tim Apr 05 '17 at 14:04