18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

selecting a list of elements via java driver

edited November -1 in Sahi - Open Source
My second question of the day! :) I am using sahi via the Twist testing tool. So I'm using the Java driver. Is there a good way to select multiple elements? I was surprised that the div and byClassName browser element selectors only return the first match. I would expect that I would be returned a List of ElementStubs that I could then filter or count, etc.

I have worked around this for now by using a while loop and the square bracket notation, here is an example of what i mean:
index = 0;
while(browser.div("my-class[" + index + "]").exists()){
  index++;
}

in this case the index variable equals the number of divs that are of class "my-class". Is there a better way to achieve this? For objects with multiple classes I have been forced to use xpath, and I've got performance concerns, especially on IE. Thanks!

Comments

  • narayannarayan Administrators
    To get the count, use

    browser.fetch("document.getElementsByClassName('my-class').length");

    None of the Sahi APIs returns a list right now. We are adding a "collect" method which works something like

    browser.collect("div", "sahi_identifier/js function") which will return a list of elementstubs. This is not available in the current version of Sahi that comes with Twist.

    Regards,
    Narayan
  • Thanks Narayan,

    I don't think IE supports getEelemntsByClassName. What do you recommend in that case?
Sign In or Register to comment.