'How to click third link of contents box of this wikipedia article?

I am trying to target 3rd link

driver.get("https://en.wikipedia.org/wiki/Cricket");
WebElement contents = driver.findElement(By.xpath("//*[@id='toc']")); //moving webdriver scope to contents
List<WebElement> allLinks = driver.findElements(By.tagName(“a”)); 
contents.click(allLinks.get(2));

Is my code correct or not?



Solution 1:[1]

I think the last line should be changed as,

allLinks.get(2).click();

Using this, you will be able to click the 3rd link element.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Methmal Godage