How to extract the dynamic values of the id attributes of the table elements using Selenium and Java

后端 未结 2 1319
半阙折子戏
半阙折子戏 2020-12-21 03:29

I have a table where each row will have a download link with a (partly) auto-generated id element. The reason for this is that the actual href-element will allways be \"#\",

2条回答
  •  伪装坚强ぢ
    2020-12-21 04:00

    Until you find the element first, you can't retrieve the attribute values of it.

    Use findElements method to fetch all links using the following locator

    table tr td[class='journalTable-journalPost'] a
    

    Then iterate through each element using for-each to fetch id for each element.

    Sample code:

    List listOfLinks = driver.findElements(By.cssSelector("table tr td[class='journalTable-journalPost'] a"));
    
    for(WebElement link: listOfLinks) {
         System.out.println("id:" + link.getAttribute("id"));
    }
    

提交回复
热议问题