2

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:

driver.findElement(By.xpath("//a[@href='/edit-files.php?i=26']")).click();
driver.findElement(By.cssSelector("a[href*='/edit-files.php?i=26']")).click();
  • 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 Jan 23 '17 at 14:12
  • OP tried css so I added it to title. – Michael Durrant Mar 24 '17 at 22:35
1

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(); 
}
| improve this answer | |
  • 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 Jan 23 '17 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 Jan 23 '17 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 Jan 23 '17 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 Apr 24 '17 at 6:09
0

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){
  e.click();
}

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

| improve this answer | |
  • 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 Jan 24 '17 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. – Thiago Fioravante Jan 24 '17 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. – Thiago Fioravante Jan 24 '17 at 14:17
  • Hi, no such element: Unable to locate element: {"method":"xpath","selector":"//a[@class='fa fa-pencil-square-o']"} thanks – Asiel Jan 24 '17 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. – Thiago Fioravante Jan 24 '17 at 19:09
0

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

enter image description here

| improve this answer | |

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