how do i use list for selenium web driver in java

I need to select id which are continuously changing, and one id is in one row.

need to select id from each

Edit :

 <tr id="tr-132885-id" class="bgwhite" onmouseout="hilightOnMouseOver('tr-132885-id','out')" onmouseover="hilightOnMouseOver('tr-132885-id','in')"> 
      <td class="companynm-td" valign="top"> <div id="expand-132885-div" class="left" style="vertical-align:top;"> 
         <a onclick="expandCompanyDetail('185159','132885','show','current','1');return false;" href="#">
  • 2
    paste yours html code we try to answer Commented Dec 21, 2015 at 9:21
  • This question doesn't clearly reflect what you are trying to ask. Please add more details Commented Dec 21, 2015 at 10:19
  • 1
    It can be done easily by xpath (by position). But we need to see your HTML and also if you have any tried any code so far?? Commented Dec 21, 2015 at 12:48
  • I guess id is dynamic which gets changed everytime,you need to get the pattern of the id, and iterate over it
    – saikrishna
    Commented Dec 22, 2015 at 7:13
  • yes ID is dynamic which are changing for each row
    – PRasd
    Commented Dec 23, 2015 at 6:21

3 Answers 3


Below is a way to list URLs, you can change the By.tagName to your required selector like By.id

WebDriver driver = FirefoxDriver();   
List<WebElement> elements = driver.findElements(By.tagName("a")); 

Hope this will help you out.


Ask your developers to add a name (which does not need to be unique) to relevant elements. find_elements (Java may have different spelling) returns a LIST of elements.

If you cannot have name, you can find by CSS class. Xpath is locator of last hope, too brittle and too slow.

  • Yes! If locating an element is that difficult, its better to ask the developers to put unique, static and less fragile attributes to locate the element.
    – Faiz
    Commented Dec 23, 2015 at 8:37

If you want to store each and every ID from all of the rows or columns then look at the below code :

Step1: grab the table, Step2: grab the rows, Step3: grab the columns.

WebElement table=driver.findElement(By.id("someID"));
//you can choose id,xpath or any other unique locator.
List<WebElement> rowID=table.findElements(By.tagName("tr"));
for(WebElement row:rowID){
    List<WebElement> colID=row.findElements(By.tagName("td"));

now colID and rowID contains ID's of columns and rows respectively.

Your Answer

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

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