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.

Get text from table cell

simi823simi823 Members
edited April 2013 in Sahi - Open Source
Please bare with me as a newbie...

The div tag id in a table cell generates a unique number like so:
<div id="standing_division_wins_458873" class="editable_clean">8</div>
The 458873 is generated for other values in the table.
I can get the text of that cell using this:
_getText(_cell(_table("searchresults_table"), 2, 5));
or similar code for the other rows and columns. But, the text returned is this:
226 new Ajax.InPlaceEditor('standing_division_points_458873', '/reporter/ajax/inline/edit/standing/458873/division_points/', {size:4});

I need the 226 value only, but it varies in length in the different cells I access.

How else can I get only the div tag text, since I can't use the div id because the last number of the id varies?

Best Answer

  • Answer ✓
    Hi Simmi,
    I have used row name/id and the column idex, from which the value to be accessed, to get a cell value... I was able to get a cell value using the below one line code...
    var $dealerAddress = _getText(_row("dealers_tr3").cells[6]);
    here, I have saved the cell value (in column 7 of row number 3 in dealers table) to a variable to compare it on next page. I hope this will help...

    here "dealers_tr3" is the row identification and [6] is the column index.

Answers

  • Or, get the text in the table cell from the div tag that _containsText("standing_division_wins_")? Is that possible?
  • gaveyomgaveyom Members
    edited May 2013
    Hi Simi,

    Pl try following code
    
    
    _set($tableData, getTableContents(1));
    
    
    <browser>
    
        // Fetches table contents. contents will be accessible via indexes. 
        function getTableContents(id){
            var array2d = new Array();
            var x=document.getElementsByTagName("table");
            var rows = x[id].rows;
            
            for (var i=0; i<rows.length; i++){
                var row = rows[i];
                array2d[i] = new Array();
                var cells = rows[i].cells;
                for (var j=0; j<cells.length; j++){
                    array2d[i][j] = _getText(cells[j]);
                }
            }    
            return array2d;
        }
    
    </browser>
    
    
    
  • simi823simi823 Members
    graveyom -- when you use the <browser> tag, does that mean that the function should be saved in the html that I'm testing? I do not have access to that page or it is already built and cannot modify with my sahi script. Therefore, I need to read the table data within the sahi script I am testing on the website.
  • simi823simi823 Members
    So I tried graveyom's suggestion in my sahi script but the function was not recognized. I don't know what I'm doing wrong.

    tsiva suggestion was great and it works in the sahi controller, but running the script give me this error:
    _assertEqual(_getText(_table(0).rows[3].cells[1]), "7"); TypeError: _sahi._table(...).rows[3]

    Though, using this _assertEqual(_getText(_table(0).rows[3].cells[1]), "7"); in the record mode of the sahi controller's Evaluate Expression gives me True.

    I'm using Firefox and here is the snippet of script from the sahi script I am running:
    for (var $a=0; $a < $away_scores.length; $a++) {
    var $selectCount = 1;
    if ($a == $away_scores.length) {
    _assertEqual(_getText(_table(0).rows[3].cells[$selectCount]), $row[8]);
    } else {
    _assertEqual(_getText(_table(0).rows[3].cells[$selectCount]), $away_scores[$a]);
    }
    $selectCount += 1;
    }

    So confusing why this is not working!!!
  • tsivatsiva Members
    Hi Simi823,
    It seems you are using row number instead of row_id. Could you please check with row_id and see whether that works?
    I am also not sure whether _table().rows().cells() will works in sahi.
  • simi823simi823 Members
    edited May 2013
    Hi tsiva,

    Thank you for the quick reply. OK, so then I add to my script the following to test it out:
    _alert(_getText(_cell(_table(0), 3, 1)));
    And then get the following error:
    _alert(_getText(_cell(_table(0), 3, 1))); Error: The parameter passed to _getText was not found on the browser
    But, again when I test it in the Evaluate expression of the controller it works fine. I don't know if this makes a difference, but when I run the script it starts on one page, then opens up a link to another page in a new window. Do you think this would have something to do with the problem? I am so frustrated at this point!!!

    UPDATE: It was because of the separate page, so nevermind!!! I figured it out. I was using _click to the link rather than use _navigateTo. Rookie mistake!!!
Sign In or Register to comment.