5

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?

2

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.

0

Check out my update to an older Stack Overflow question on using Selenium 3 using store xpath count and if...end to execute code.

The basic idea:

store xpath count | css=.something | how_many_somethings if | ${how_many_somethings} < 1 ...magic stuff... end

The other question has a screen shot and link to a full article.

-1

Generically:

try {
       waitForElementToBeVisible
       //Do nothing
} catch (ElementNotFound) { click }

If no JavaScript wait is necessary:

if( ! element.IsVisible()) click;

//Continue with your life ...

1
  • 1
    The question is about Selenium-IDE – Alexey R. Oct 8 '18 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.