0

I have a scenario to select 8 players and at the same time click their positions.Scenario like this double click a player first, after selection there is window for selecting player position..

here my code will take all players and printing their names... I am doing that first select the table and select the row and then select column to take the each players in the list...

 try {
             Thread.sleep(4000);     
             WebElement table = driver.findElement(By.xpath(".//*[@AutomationId='lsvAwayTeamHitters1']"));
             List<WebElement> row= table.findElements(By.xpath("./*[contains(@LocalizedControlType, 'item')]"));
             int row_size = row.size();
             System.out.println(row_size);

             int col_size = 0;
             for(int i=0;i<row.size();i++)
             {

             List<WebElement> col = row.get(i).findElements(By.xpath("./*[contains(@LocalizedControlType, 'text')]"));
                 col_size = col.size();
                 System.out.println(col_size);

                 for(int j=0;j<col.size();j++)
                 {
                     Thread.sleep(40000);
                     String celtext = col.get(j).getAttribute("Name");
                     System.out.println(celtext);
                     Thread.sleep(4000);

                 }
                 Thread.sleep(20000);
             }
         }
         catch (Exception e) {
             System.out.println(e);
         }

      }
} 

But now I want to select 8 players in the list, initially i do double click a plyer and then select their position for a click....

this is the player selection.. code..

Actions act = new Actions(driver);
 act.doubleClick(elem.get(j)).build().perform();

this is for select their position...

List<WebElement> rowCount= driver.findElementsById("PlayerPositions");
            rowCount.get(i).click();
  • act.doubleClick(elem.get(j).findElement((By.name(Integer.toString(j))))).build().perform(); doesn't seem correct – demouser123 May 3 '18 at 9:24
  • 1
    Are you using some extenstion of a WebDriver? What is findElementsByName method doing and why are you casting it to List<WebElement> ? You get index outofBount exception because your list is empty, I mean no elements were found in the very first line. – Alexey R. May 3 '18 at 10:26
  • @MivaScott please check my question...I'm stuck..please help me...sqa.stackexchange.com/questions/33791/… – user32519 May 26 '18 at 15:53
0

Looking at your code:

List<WebElement> elem= driver.findElements(By.id("lsvAwayTeamHitters1"));

You're creating a List<> of 1, which is why you're getting the error: java.lang.IndexOutOfBoundsException: Index 2 out-of-bounds for length 1

It says right there that the it has a length of 1 (one record) and you're trying to access index 2, which is out of bounds.

You are trying to find elements By.id(). By design, id's are (or at least should be) unique to a page. So if your List<> is searching for an id, it will only find the one element so you will only ever have elem.get(0).

Once you find a better way to identify all the elements you want to use, then you can loop through the List<> and do what you need to do.

int elemSize = elem.size();
for (int i = 0; i < elemSize; i++) {
    // Do stuff here
}
  • @Anthony, you are iterating through the list with i, but the statement where you access the elements you use j - act.doubleClick(elem.get(j)).build().perform(); Also, you're still using ID to mind a list of elements. I suggest rethinking how you're approaching this. – MivaScott May 4 '18 at 5:23
  • At this point, there is nothing more that can be done without more details; such as the HTML you're trying to read. This solved the original question; an error with the loop. I suggest selecting an answer and opening up a new question with the new problem with HTML and your code included. – MivaScott May 4 '18 at 6:00
  • can you check my question that i have edited – user32519 May 5 '18 at 5:54
0

Why are you writing as driver.findElementsByName("0-9"), the proper way to write is driver.findElements(By.id("a"));(Just an example).

If you are using specific methods the please don't mind. As Alexy.R mentioned the error is because the List contains 0 elements are you are accessing from 2. So it is better if you move

List<WebElement> elem= (List<WebElement>) driver.findElementsByName("0-9");

this code into the try-catch and check. Is there any specific reason for starting the loop from 2?. Before that add a Thread.sleep(4000); and check whether it is working. Since you said after clicking it opens a pop-up, it may take few seconds to move into the new window. Also check whether the elements in the pop-up is verified correctly.

Check the below code,

       try {
          Thread.sleep(4000)
          List<WebElement> elem= driver.findElements(By.id("lsvAwayTeamHitters1"));

        for(int i=2;i<elem.size();i++) 
        {
            for(int j=2;j<=i;j++)
            {
                Thread.sleep(20000);
            Actions act = new Actions(driver);
            act.doubleClick(elem.get(j)).build().perform();

            }
            Thread.sleep(20000);
             List<WebElement> rowCount= driver.findElementsById("PlayerPositions");
            rowCount.get(i).click();
        }          }
       catch (Exception e)
        {
           System.out.println(e);

        }
       }

Please run the code and if error exists please paste the error in the question. So that we can check from which line the error is occuring. Note :- If you can please paste the html for the same.

  • @Anthony Can you pls add the html. – kripindas May 3 '18 at 12:37
  • Did you mean web application? – kripindas May 3 '18 at 12:52
  • @Anthony Without the relevant html I will not be able to check the issue. – kripindas May 3 '18 at 12:53
  • can you check that i have edited – user32519 May 5 '18 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy