How to save a list of WebElements from a table to a list when implementing selenium webdriver?

I am trying to save a column of elements in a list from a table structure with the expression below, on which I need to perform a click operation to test these buttons.

The code:

I have a value for Total_element = 37.

for(int start=0; start <= Total_element; start++)  
{  
    int startn=start+1;  
    System.out.println(start);  
    List <WebElement> Element1 = new ArrayList<WebElement>() ;

    try{  
        Element1.add(Naveen.findElement(By.xpath(".//*[@id='data_grid']/tbody/tr["+startn+"]/td[2]/a/img")));  
    }catch(Throwable t){  
        System.out.println(t);  
    }  
    System.out.println(Element1.get(start));  
    System.out.println("The element" + start + "is :"+ Element1.get(start));  
    Naveen.findElement(By.xpath(Element1.get(start).toString())).click();  
    Naveen.findElement(By.xpath(".//*[@id='action']/a/span/div")).click();  
    System.out.println("The element" + start + "is :"+ Element1);  
    Thread.sleep(5000);  
}

Error:

when I try to extract items from a list, I get the following output:

[[FirefoxDriver: firefox on XP (586a8f1f-f784-4ae7-adf5-5f920dfad8e0)] -> xpath: .//*[@id='data_grid']/tbody/tr[1]/td[2]/a/img]

in which I say that my operation is failing.

+5
source share
2 answers

In fact, what happens means that the return type

   driver.findElemnt(By.xpath("xpath"));

is a WebElement. While you add the above code to ArrayList, it will add WebElement. WebElement contains information about

Driver Used - FirefoxDriver
Browser session value - 586a8f1f-f784-4ae7-adf5-5f920dfad8e0
locator used - xpath:.//*[@id = 'data_grid']/tbody/tr [1]/td [2]/a/img]

-, . , , .

Xpath locator ArrayList. .

 ArrayList<String> Element1 = new ArrayList<String>();
 Element1.add(".//*[@id='data_grid']/tbody/tr["+startn+"]/td[2]/a/img");

 driver.findElement(By.xpath(Element1.get(`startn`))).click();
+1

:

Naveen.findElement(By.xpath(Element1.get(start).toString())).click();

:

Element1.get(start).click();
+1

All Articles