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 check if element exist?

ewaewa Members
edited November -1 in Sahi - Open Source
Hi All,
I don't know How to check if the page element 'select' or 'input' exist on page source and if not exist go forward with the scripts without errors, is it possible?
Example I have 2 Tags to distinguish:
document.getElementsByTagName('select')[0]
document.getElementsByTagName('input')[5]
I don't need values witch were put into those fields I only would like to know if they exist.

Comments

  • any updates on this??
  • i got something that counts the number of inputs in a page

    var $ctr = 0;
    var $inputElement = document.getElementsByTagName('input');

    for(var $i=0; $i < $inputElement.length; $i++)
    $ctr++;

    i dont know if this will be ok for you.....

    if ($ctr == 0)
    //if not exist just do something that does not affect script
    else
    // i dont know what you are going to do here.....
  • Hi,

    Try if this works:
    if(_condition(document.getElementsByTagName('select')[0] != null))
        do something;
    else
        do something else;
    
    Regards,
    Pankaj.
  • narayannarayan Administrators
    Does this work?
    if (_condition(_select(0) != null)){
      do x;
    }
    else{
      do y;
    }
    
    For input, you could either do
    if (_condition(document.getElementsByTagName('input').length > 6 && document.getElementsByTagName('input')[5] != null))
    
    or 
    
    if (_condition(_text(5) != null)) // Use the correct type of input tag.
    
  • ewaewa Members
    Thank you.

    It works and It can be closed.
This discussion has been closed.