Antonio Diaz
Antonio Diaz

Reputation: 173

Selecting a item of a table? with Selenium

I'm tryng to select a item ( the first phone, whatever it is) in the o2 web page. I'm in https://www.o2.co.uk/shop, I select the paymonthly tab, but I'm not able to select the first phone in the list, I'm trying doing:

        List<WebElement> linkElements = driver.findElements(By.className("tab-contents"));
    String[] linksText = new String[linkElements.size()];
    int index = 0;
    for(WebElement element : linkElements){

        linksText[index++] = element.getText();
        if(index==1){
            element.click();
        }
    }

But it's not running. Anyone can help me ? I'm learning how to use selenium, therefore, I dont have many experience in it. Sorry if that is so easy to ask.

Thanks in advance

Upvotes: 1

Views: 61

Answers (1)

Florent B.
Florent B.

Reputation: 42518

Your selector returns the container for all the phones. To get the link container for each phone:

List<WebElement> linkElements = driver.findElements(By.cssSelector(".tab-contents a"));

Upvotes: 1

Related Questions