1
public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.setProperty("webdriver.chrome.driver", "C:/Automation Stuff/ChromeDriver/chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        ArrayList<WebElement> links = new ArrayList<WebElement>();
        ***links = driver.findElements(By.tagName("a"));***

    }

}

I'm new to automation, just wanted to know why driver.FindElement(ByTagName("a")) is not storing the results in ArrayList.

1

You are trying to add elements to ArrayList from a function driver.findElements(By.tagName("a")) that returns object type List.

Every ArrayList is a List, but not every List is an ArrayList.

List<Integer> list = new ArrayList<Integer>();

ArrayList<Integer> aList = new ArrayList<Integer>();

Try Using

List links = driver.findElements(By.tagName("a"));

for(WebElement link : links)
{

  System.out.print(link.getAttribute("href"));

}
| improve this answer | |
0

Since findElements uses WebDriver as generic type, List is an optimum choice.

findElements(By by)
Find all elements within the current context using the given mechanism.
java.util.List<WebElement>

findElements()

  1. On Zero Match : return an empty list
  2. On One Match : returns list of one WebElement only
  3. On more than One Match : returns list with all matching instance
| improve this answer | |
  • what is the exact difference between ArrayList and list ? – Kriti Gupta Jan 31 '17 at 12:26
  • List is a more abstract version of ArrayList. like Animal is a more abstract version of Dog, Cat etc – the_coder Jan 31 '17 at 13:17
0

Not familiar with Selenium in Java but I think that the return type is List<WebElement>

Use:

List<WebElement> elements = driver.findElements

or convert ArrayList to List

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.