I need to click on one of many links which are similar the only difference is the number. Can somebody help me with that? (using Java)

<a href="/edit-files.php?i=26"><i class="fa fa-pencil-square-o"></i></a>
<a href="/edit-files.php?i=21"><i class="fa fa-pencil-square-o"></i></a> 

I tried these didnt work:

  • example of the links that are all the same: <a href="/edit-files.php?i=21"><i class="fa fa-pencil-square-o"></i></a> <a href="/edit-files.php?i=26"><i class="fa fa-pencil-square-o"></i></a>
    – Asiel
    Commented Jan 23, 2017 at 14:12
  • OP tried css so I added it to title. Commented Mar 24, 2017 at 22:35

3 Answers 3


If the only difference is the number after ?i= then you can simply concatinate the Xpath in Java. So if you want to iterate over the first ten links and click on each of them:

for(int idx = 1; idx <= 10; idx++) {
  driver.findElement(By.xpath("//a[@href='/edit-files.php?i=" + idx + "']")).click(); 
  • Thanks for your quick response. unfortunately it didn't work for me . I can hard coded the value in the code but the xpath seems to be not work with the ?= i just want it to work with hard coded if you have any other idea plz share thank you (these numbers after ?= are randomly generated so they didn't start with 1 to add only to get the next link
    – Asiel
    Commented Jan 23, 2017 at 16:11
  • Could you be more specific as to what doesn't work? I tried a very simple HTML: <html> <body> <a href="1.html">1</a> <a href="2.html">2</a> </body> </html> and that works: driver.findElement(By.xpath("//a[@href='1.html']")).click();
    – ralph.mayr
    Commented Jan 23, 2017 at 16:27
  • click on icon should open another page the icon is in the <i> tag but the link in the <a> tag this page can not be opened through my automated test :( . <td> <ul class="actionButUsers"> <li><a href="/edit-files.php?i=21"><i class="fa fa-pencil-square-o"> </i> </a> </li> </ul> </td>
    – Asiel
    Commented Jan 23, 2017 at 17:03
  • I think it is another problem Once you click on link it will open new page. There might be the links are available or not. You can back by driver.navigate().back(); and click on another links
    – Sagar007
    Commented Apr 24, 2017 at 6:09

You can find all elements by class an put them in a list. Then you iterate over the list and click the elements.

List<WebElement> list = driver.findElements(By.xpath("//a[@class='fa fa-pencil-square-o']"));
for(WebElement e : list){

PS. Note the diference between 'findElement' and 'findElements'

  • Hi , I really appreciate your replay. I tried that the page that suppose to be loaded didn't load. Im OK if I can only open one link . Do you have any other ideas . Thank you
    – Asiel
    Commented Jan 24, 2017 at 13:14
  • Ok, what exactly didn't work? It found the elements? Or returned an exception (like NoSuchElementException or StaleElementReferenceException)? Ans what happens when you click a link? If the first link loads a new page or opens a modal and you try to click the second one it will fail. Commented Jan 24, 2017 at 14:15
  • If you want to open only one link, you can use the same xpath but with 'findElement' (without the 's'). findElement ALWAYS return the first element it finds. This way you'll click the first link always. Commented Jan 24, 2017 at 14:17
  • Hi, no such element: Unable to locate element: {"method":"xpath","selector":"//a[@class='fa fa-pencil-square-o']"} thanks
    – Asiel
    Commented Jan 24, 2017 at 17:00
  • Ok, try this xpath then: "//a[contains(@class, 'fa-pencil-square-o')]". I like to use a Chorme extension to validate my xpaths called "XPath Helper", you can use it. Commented Jan 24, 2017 at 19:09

Specify the link you want to click. It works for me, e.g.

enter image description here

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