How can I check if an element exists in an if statement?



  • I'm using Selenium IDE for Chrome and I'm trying to click somewhere only if a specific element doesn't exist.

    If I only wanted to do that, I can do it with an assert:

    assert element not present    css=.something
    click                         css=.button
    

    However, this is part of a larger script and I want to continue with the rest of the script regardless of whether the element exists.

    So I want to do something more like:

    if          {something which means roughly "assert element not present"}
       click    css=.button
    end
    

    Is this possible? How do I do this?



  • You can do this with if/endif of the kantu selenium ide:

    verifyElementPresent (locator)
    if | !{!statusOK}
     Click (...)
    endIf
    Script continues... 
    

    => The "trick" here is to use !statusOK to check in the status of the last command. ${!statusOK} here it will be true if the element is found by verifyElementPresent. The ! in front of the variable inverts this. So the Click is reached when the element is not found.


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2