verifyElementPresent returns false in my macro, but true if I execute the line alone — why?!

Having a weird issue with a relatively simple script I’m writing. All I want to do is click all “Load comments” and “Load replies” links on a page so that everything is visible.

There are two do loops in my script. The first works fine, but the second behaves strangely. If you run the macro, it erroneously returns “false” and doesn’t click the “Load x replies” link, but if you right click the second do line and “Execute from here”, verifyElementPresent for xpath=//button[contains(text(),'Load’)] correctly returns “true” and everything goes as planned, with the script clicking the “Load x replies” link, doing one more loop, finding there’s no more links to click, and finishing.

What could be the reason it gets gummed up when I run the whole macro? Is the first verifyElementPresent leaving some residue that confuses things?

Here’s my script. I wrote a comment right before the problematic line.

{
  "Name": "Download comments from Patreon",
  "CreationDate": "2020-7-27",
  "Commands": [
    {
      "Command": "open",
      "Target": "https://www.patreon.com/posts/time-out-page-12-39526198",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "Loop over the page to load all comments",
      "Value": ""
    },
    {
      "Command": "do",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "verifyElementPresent",
      "Target": "css=button[data-tag=\"loadMoreCommentsCta\"]",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${!statusOK} == true",
      "Value": ""
    },
    {
      "Command": "click",
      "Target": "css=button[data-tag=\"loadMoreCommentsCta\"]",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "verifyElementPresent",
      "Target": "css=button[data-tag=\"loadMoreCommentsCta\"]",
      "Value": ""
    },
    {
      "Command": "repeatIf",
      "Target": "${!statusOK} == true",
      "Value": ""
    },
    {
      "Command": "verifyElementPresent",
      "Target": "xpath=//button[contains(text(),'Load')]",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "Loop over the page to load all replies",
      "Value": ""
    },
    {
      "Command": "do",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "This next verifyElementPresent returns false when running the whole macro, but true when \"Execute from here\" is run afterwards. Why!",
      "Value": ""
    },
    {
      "Command": "verifyElementPresent",
      "Target": "xpath=//button[contains(text(),'Load')]",
      "Value": ""
    },
    {
      "Command": "if_v2",
      "Target": "${!statusOK} == true",
      "Value": ""
    },
    {
      "Command": "click",
      "Target": "xpath=//button[contains(text(),'Load')]",
      "Value": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "verifyElementPresent",
      "Target": "xpath=//button[contains(text(),'Load')]",
      "Value": ""
    },
    {
      "Command": "repeatIf",
      "Target": "${!statusOK} == true",
      "Value": ""
    }
  ]
}

You almost found the solution yourself :wink:

Once ${!statusOK} is changed from true to false, it does not itself reset to true during the macro run.

Solution:

After your first !statusOK check, add

store | true | !statusOK

That did it! Thank you so much for this. I didn’t realise it worked that way.

You can also use {!lastCommandOK} instead of {!statusOK}, which checks only the previous line in the macro. This way you don’t have to keep resetting the overall status, which could be reserved for reporting on your macros end result.

1 Like