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.

How to get element by partial text?

jkbjkb Members
edited February 2013 in Sahi - Open Source
I am using the function _containsText to help me identify labels that are not complete and I would like to know if it is possible to return the element ID of the offending label.

Example:

Label on screen says "file not found". I run my script and results tell me that "not found" is on the screen. I would like to return the whole label - "file not found".

Any ideas?

Answers

  • With $element.id you can get the .... http://dundundun.net/ ... elementID! ^^

    is that what u asked for?
  • jkbjkb Members
    I'm not sure what you mean @globalwarming; could you provide the syntax? I would like to look for a partial title and get back the element(s) that match it.
  • ok, if I understand right, you look for all "not found" which are in a heading?
    you could use the _collect API to gather all elements and return the id
    // do that for heading2, div... whereever you look
    // you my need an inElement argument
    // see http://sahi.co.in/w/browser-action-apis for collect API
    $elements= _collect('_heading1', '/not found/');
    // log element id
    for ($i=0; $i < $elements.lenght; $i++){
      _log("Not found in element" + $element.id);
    }
    

    does that solve your problem?

    Regards
    Wormi
  • jkbjkb Members
    Is there a way to look across types? My text could be in a div, a heading, etc.
  • i guess not... you have to provide a type for the collect API. You could try something like
    _collect('/./', '/not found/');
    and please tell me if it worked :)

    alternatively you could write a recursive funtion which crawles through your DOM tree
    something like
    function logNotFoundOccurrences($element){     
        var $element = $element || document.body;     
        if ($element.hasChildNodes()) {       
          var $child = obj.firstChild;
          if (_getText($child).match("not found")){
              _log($child.id) 
          }
          while(child){         
              logNotFoundOccurrences($child);         
          }
          if ($child.nextSibling) {
            logNotFoundOccurrences($child.nextSibling);
          } else {
            return
          }
       }
    }
    
    // looks good but has not been tested ;)
    // some _condition may be missing here and there
    
  • qawebqaweb Members
    Thanks for your helpful reply here and in another _collect forum note.
    I'm a bit stymied by the syntax of the _collect function
    · · _collect(apiType, id, inEl)
    ... seems somewhat er uh succinct.
    1. what do the "forward slashes" "/" indicate? (that regex syntax will work??)
    2. what are [acceptable] "apiTypes"s - examples shows only "_link" which led me so far to successfully try similar HTML based items "_image" and "_div" and "_span".
    3. If I buy the PRO version is the manual a bit more detailed?
    ^_^
    (You can tell I'm enjoying this, close to success, because my questions are so picky *and* picayune!)
  • Hi and thanks, I am glad to help

    3. You can get the Pro Docs here, but it does not provide many more information on _collect http://sahi.co.in/w/file_download/5/SahiPro_V4_2_Documentation.pdf

    2. The API types are everything Sahi recognizes like _link, _div, _span, _table, _cell ..... when you can access it with the Sahi Controller, you can collect it :)

    1. The forward slashes are part of the regular expression, have a look here http://www.w3schools.com/jsref/jsref_obj_regexp.asp

    Regards
    Wormi
Sign In or Register to comment.