0

How to find variable elements on Webpage using selenium webdriver.

I tried to list data from the anchor tag. I need for particular elements generated on the page in particular section.

public void delete()
{
    driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
    driver.findElement(By.id("teamsummary")).click();

    List<WebElement> list = driver.findElements(By.tagName("a"));
    System.out.println("Number of links: " + list.size());

    for (int i = 0; i < list.size(); i++) {
        System.out.println(list.get(i).getText());
    }
}
  • What do you mean? Please reformulate the question. Give us some more context. – Alexey R. Jun 7 '18 at 8:30
  • On a webpage I am adding team names like test1,test2,test3 and so on But the teams generated have dynamic ID(16 digit) and cant use dynamic id to find element as it always changes and after every loop I have to delete all the teams one by one by selecting them. I guess this description will help you – Pardeep Kochhar Jun 7 '18 at 8:40
  • If the team name remains the same, you can find the element by linkText. Use driver.findElement(By.linkText("team"+i)).click(); Put that line in FOR loop where i changes after every iteration. – Rishikesh Jun 7 '18 at 9:01
  • Team name cant be same team name must be different. – Pardeep Kochhar Jun 7 '18 at 11:57
  • 1
    That is why we use 'team'+i. The value of i will change for each iteration. So for 1st iteration, we will search for team1, then for 2nd iteration, it will search for team2. You can name your teams, team1, team2, team3 for ease in finding them. Or you can use partial linktext.. – Rishikesh Jun 7 '18 at 13:27
2

You can use partialLinkText to find elements having name 'team'. So if you name your team - team1, team2,... You can find them by using partialLinkText.

public void delete(){

  driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
  driver.findElement(By.id("teamsummary")).click();
  List<WebElement> list = driver.findElements(By.partialLinkText("team"));
  System.out.println("Number of links: " + list.size());
  for (int i = 0; i < list.size(); i++) {
    System.out.println(list.get(i).getText());
}

Check if this solves your problem

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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