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());
    }
}
5
  • What do you mean? Please reformulate the question. Give us some more context.
    – Alexey R.
    Jun 7, 2018 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 Jun 7, 2018 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, 2018 at 9:01
  • Team name cant be same team name must be different. Jun 7, 2018 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, 2018 at 13:27

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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