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.

script for collecting data from html table

rajatsdrajatsd Members
edited November -1 in Sahi - Open Source
I have a urgent requirement to collect all the pincode data from India post wbsite but despite of all my efforts I am unable to get the data avaialble in cells of a table.

Is there any connection of it to the security of the webpage,which is not letting me return data from its table? I have applied normal Javascript for retrieving data but all n vain.

Please send me some piece of code to check if what I am doing is wrong.

Comments

  • narayannarayan Administrators
    Hi Rajat,

    Have a look at sample code in sahi/userdata/scripts/demo/table_2darray.sah

    Regards,
    Narayan
  • Thanks a lot.
  • Now since I have collected the data in a 2d array , now I want to write it to a csv for which I am using _writeCSVfile($arrayEle[$k],"c:\\Program Write.csv"). But it doesn't seem to work.
  • rajatsd wrote:
    Is there any connection of it to the security of the webpage,which is not letting me return data from its table?


    Please answer me as when I use the array to display data on a textbox avaialbe on the webpage, it works fine but my every attempt to store the array data somewhere on my system results in failure.
  • narayannarayan Administrators
    Hi Rajatsd,

    It is not a security related thing. What do the Sahi console and Sahi logs show?
    What is $k in
    _writeCSVfile($arrayEle[$k],"c:\\Program Write.csv")
    ?
    Try to write to a file name without spaces.

    Regards,
    Narayan
  • _click(_image("img3"));
    _setSelected(_select("ddl_state"), "ANDHRA PRADESH");
    _click(_submit("Search[2]"));
    var $group = 0;
    <browser>
    function getTableContents(table){
        var $array2d = new Array();
        var $rows = table.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>
    for(var $irows = 0; $irows < 16112; $irows+=100)
    {
          if($group == 1)
                _click(_link("..."));
    
          for( var $igroup = 1; $igroup < $group; $igroup++)
             {
                _click(_link("...[1]"));
             }
          for(var $ipage = ($group*10)+1; $ipage <= ($group*10)+10; $ipage++)
           {
                if($ipage != ($group*10)+1)
                      _click(_link("" + $ipage));
    
                 for($rows = 0; $rows < 10; $rows++)
                   {
                      if($rows == 0)
                        {
                          _click(_link("Select"));
                         var $str = "";  
                         var str1 = "";
                         var $tab = document.getElementById('dvw_detail');
                         var $arrayEle=new Array();
                         $arrayEle=window.getTableContents($tab);
                         _setValue(_textbox("txt_stateon"),$arrayEle[0][1]);
                         _writeCSVFile($arrayEle[0][1]+"" , "C:\\Pincode2.csv", true);
                         _writeCSVFile($arrayEle[1][1]+"" , "C:\\Pincode2.csv", true);
                        } 
                        else
                        {
                         _click(_link("Select["+$rows+"]"));
                         var $str = "";
                         var $str1 = ""; $tab = document.getElementById('dvw_detail');
                         var $arrayEle= new Array();
                         $arrayEle=window.getTableContents($tab);
                         _setValue(_textbox("txt_stateon"),$arrayEle[0][1]);
                         //The _setValue sets the value of array element at the desired index to 
                         //the textbox namely "txt_stateon" available on the webpage.
                         var $str = $arrayEle[0][1];
                         _writeCSVFile($arrayEle[0][1] +"", "C:\\Pincode.csv", true);
                         _writeCSVFile($arrayEle[1][1] +"", "C:\\Pincode.csv", true);
                         //The _writeCSVFile writes null when +"" is removed and with it it writes 
                         //window.getTableContents(document.getElementById('dvw_detail'))[0][1].
                        }
                        if($group !=0)
                            _click(_link("..."));
                        if($ipage !=($group*10)+10)
                           {
                            for( var $igroups = 1; $igroups < $group; $igroups++)
                                {
                                    _click(_link("...[1]"));
                                }
                           }    
                    }
                    
            }
            $group++;
    }
    
    

    find the commented lines in the code area and let me know why the behaviour of array element differ in each case.
  • narayannarayan Administrators
    Hi Rajatsd,

    A few points to note:

    1) _writeCSVFile takes a 2D array as input. So if you have a 2D array, you can use a single call to write the contents to file.

    _writeCSVFile(array2d, filePath, overwrite, wordSeparator);


    2) As specified in the example, you need to fetch the contents of the table from the browser using _set.

    So your code should look like:
    var $arrayEle;
    _set($arrayEle, getTableContents(document.getElementById('dvw_detail')));
    _writeCSVFile($arrayEle, "C:\\Pincode.csv", true);
    

    Let us know if that works.

    Regards,
    Narayan
  • It worked. and I am very thankful to you.


    Thanks
  • narayannarayan Administrators
    Hi Rajatsd,

    You are welcome!

    Regards,
    Narayan
This discussion has been closed.