Go back to the top and try again if object not found

Hello.

I have a loop running, and each round has the next row pulled from a CSV.

Sometimes a webpage takes a while to load, and that’s OK, but when that happens, I’d like to restart the macro, but only from the last round/row (not all over.) Can we have the round start over?

In other words, how do I write: if X (image) is not found - go back to the top, if X is found - continue.

I tried using !statusOK, but I’d need to clear/reset the error once the macro restarts from the top, otherwise it keeps looping because as soon as there’s an error, it keeps pointing back to a LABEL I set.

I really hope I was clear. It’s not easy to describe this stuff.

Thanks in advance!

Kantu has

OnError | #goto | (label)

and

OnError | #restart <== does this work for you? It restarts the macro if an error happens.

Will #restart go back to the most recent row in the CSV?

In other words, I don’t want to go back to Round 1/100, but back to Round X/100, where X is where the error occurred.

Also, where does that line go? I don’t want to restart just for any error, but only particular commands. I figured I’d need to set ErrorIgnore to true and specify where I don’t want to ignore the errors.

Is my goal achievable?

Thanks.

This is doable, if you take care of the line counter yourself. For this use a global variable (= starts with “global…”) so it survives between the loops.

At the end of the macro, increase it (“+1”) to the next line. This increase only happens if the macro loop reach the end successfully. Otherwise (in case of error) the same CSV line is used again.

Thank you so much!

Where does “OnError | #restart” go? Anywhere?

And what if I DO want to stop the macro for a particular command error, is that possible?

Yes, before the error, so ideally on top.

But actually, since you use loops, you do not need this command at all! Just make sure the “Continue next loop” is checked for loop errors:

contnextloop

What errors do you have in mind? In any case, you can use !errorignore = true for certain sections. Errors while !errorignore = true do not trigger the error handling, and you can manage them yourself, for example with gotoIf !statusOK != "true" and then handle it.

I followed your screenshot example, but when the loop restarted it came back to line1 of the CSV…

Here’s what I have at the top:

{
  "Command": "if",
  "Target": "${!loop}==1",
  "Value": ""
},
{
  "Command": "store",
  "Target": "1",
  "Value": "globalCurrentLineToRead"
},
{
  "Command": "endif",
  "Target": "",
  "Value": ""
},
{
  "Command": "store",
  "Target": "${globalCurrentLineToRead}",
  "Value": "!csvReadLineNumber"
},
{
  "Command": "onError",
  "Target": "#restart",
  "Value": ""
},
{
  "Command": "csvRead",
  "Target": "readcsvtestdata.csv",
  "Value": ""
},

Can you please paste here the complete macro that you are using for testing? For example, did you add the globalCurrentLineToRead = globalCurrentLineToRead +1 part at the end?

(Of course, you can skip the part where you are filling in data in your website.)

Yes, I added the +1 toward the end (does it have to be the last line?), but the problem is not that it didn’t add up; The error occurred on row 9, and when the loop restarted, it came back to row 1.

{
“Command”: “storeEval”,
“Target”: “${globalCurrentLineToRead}+1”,
“Value”: “globalCurrentLineToRead”
},

Ok, I found the issue:

  • When you use OnError #restart, it restarts the macros completely. Technically this is exactly the same as pressing the Play button - or here the Loop button - again. That is why it starts again with !loop=1.

  • But without OnError #restart it works as it should! Then the automatic loop error handling takes over, and if one loop has an error it continues with the next loop.

In the macro below I simulate an error in loop = 3. The macro then continues with loop=4. And in loop = 4 we successfully read line 3 of the CSV file, since we keep track of the CSV line number in the variable globalCurrentLineToRead).

{
  "CreationDate": "2018-6-22",
  "Commands": [
    {
      "Command": "store",
      "Target": "fast",
      "Value": "!replayspeed"
    },
    {
      "Command": "comment",
      "Target": "#restart",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "The file ReadCSVTestData.csv is pre-installed with Kantu.",
      "Value": ""
    },
    {
      "Command": "if",
      "Target": "${!loop}==1",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "1",
      "Value": "globalCurrentLineToRead"
    },
    {
      "Command": "endif",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "store",
      "Target": "${globalCurrentLineToRead}",
      "Value": "!csvReadLineNumber"
    },
    {
      "Command": "csvRead",
      "Target": "ReadCSVTestData.csv",
      "Value": ""
    },
    {
      "Command": "open",
      "Target": "https://docs.google.com/forms/d/e/1FAIpQLScGWVjexH2FNzJqPACzuzBLlTWMJHgLUHjxehtU-2cJxtu6VQ/viewform",
      "Value": ""
    },
    {
      "Command": "type",
      "Target": "name=entry.933434489",
      "Value": "${!COL1}"
    },
    {
      "Command": "type",
      "Target": "name=entry.2004105717",
      "Value": "${!COL2}"
    },
    {
      "Command": "if",
      "Target": "${!loop}==3",
      "Value": ""
    },
    {
      "Command": "comment",
      "Target": "On loop 3 we trigger an error to test",
      "Value": ""
    },
    {
      "Command": "echo",
      "Target": "On loop 3 we trigger an error to test",
      "Value": ""
    },
    {
      "Command": "click",
      "Target": "locatordoesnotexist",
      "Value": ""
    },
    {
      "Command": "endif",
      "Target": "",
      "Value": ""
    },
    {
      "Command": "type",
      "Target": "name=entry.1382578664",
      "Value": "${!COL3}"
    },
    {
      "Command": "clickAndWait",
      "Target": "//*[@id=\"mG61Hd\"]/div/div[2]/div[3]/div[1]/div/div/content/span",
      "Value": ""
    },
    {
      "Command": "storeEval",
      "Target": "${globalCurrentLineToRead}+1",
      "Value": "globalCurrentLineToRead"
    }
  ]
}

So would I need to switch to:
“Continue next loop” for “If error happens in loop”?

Yes! (this is also the default)

Ulrich, how do i go to a certain command further down using onError command? Can i use the number of the command? This comand is an XClick with 1400,300 coordonates. Please help. Much appreciated.