Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 "#", so the id's separate the downloads.

I need to find the name of that id element in the td. That is: I know the table row has an id element, and I know part of the name, and I need to get the exact name.

I'm accessing each row one at a time, so I only need to look within ONE td at a time. No need to look through the whole table.

I know well how to find an element when I know the name. But to find the element when I only know the type is another thing.

...
<tr>
 <td class="journalTable-journalPost"
  <a class="htext-small" href="#" id="downloadJournalPost-345">Download</a>
 </td>
</tr>
<tr>
 <td class="journalTable-journalPost"
  <a class="htext-small" href="#" id="downloadJournalPost-346">Download</a>
 </td>
</tr>
...

I cannot find any method(s) in the webdriver that lets me find an element by type.

Partial name would work, since the id's get the name "downloadJournalPost-xxx", where only xxx changes. But link text is the only value I can find which lets me search for a partial match.

EDIT: More complete markup.

<td class="journalTable-journalpost">
 <span class="hb-tekst--sekundar">In <!----><est-ikon class="ng-star-inserted">
  <div aria-hidden="true" class="hb-ikon hb-ikon--pil3-inn  ">
   <svg focusable="false">
    <use xlink:href="#ikon-pil3-inn"></use>
   </svg>
  </div></est-ikon><!----></span>
 <span class="hb-tekst--mellomTittel hb-avstandIngen"> Application and attachments</span>
 <a class="hb-tekst--liten" href="#" id="lastNedJournalPost-2892">Download journal post</a>
</td>
like image 307
Hfrav Avatar asked Apr 16 '19 11:04

Hfrav


People also ask

How does Selenium handle dynamic data tables?

There is no rocket science in the handling of tables. All you need to is to inspect the table cell and get the HTML location of it. In most cases, tables contain text data and you might simply like to extract the data given in each row or column of the table.


2 Answers

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<WebElement> listOfLinks = driver.findElements(By.cssSelector("table tr td[class='journalTable-journalPost'] a"));

for(WebElement link: listOfLinks) {
     System.out.println("id:" + link.getAttribute("id"));
}
like image 86
Naveen Kumar R B Avatar answered Nov 15 '22 04:11

Naveen Kumar R B


To print the List of id attribute of the element you need to induce WebDriverWait for the visibilityOfAllElementsLocatedBy() and you can use Java8 stream() and map() and you can use either of the following Locator Strategies:

  • cssSelector:

    List<String> myID = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.cssSelector("td.journalTable-journalPost>a.htext-small"))).stream().map(element->element.getAttribute("id")).collect(Collectors.toList());
    System.out.println(myIDs);
    
  • xpath:

    List<String> myIDs = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//td[@class='journalTable-journalPost']/a[@class='htext-small' and text()='Download']"))).stream().map(element->element.getAttribute("id")).collect(Collectors.toList());
    System.out.println(myIDs);
    
like image 45
undetected Selenium Avatar answered Nov 15 '22 03:11

undetected Selenium