Move to next tab on each repeat

Hi guys. I’m trying to write a script that goes through multiple open tabs, one by one, and searches for a form to fill. My problem is, it doesn’t go further than tab 1. If I have 10 tabs open, I want the script to go one-by-one, through each tab until it finds the form and it fills it. Is that possible? Sorry. I’m new to this

Here’s my current code that doesn’t work as intended.

{
  "Name": "Draft 1",
  "CreationDate": "2023-4-16",
  "Commands": [
    {
      "Command": "label",
      "Target": "p1",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "selectWindow",
      "Target": "tab=1",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "visualSearch",
      "Target": "t1dpi_96.png",
      "Value": "x",
      "Description": ""
    },
    {
      "Command": "if",
      "Target": "${x}!==0",
      "Value": "p1",
      "Description": ""
    },
    {
      "Command": "click",
      "Target": "name=email",
      "Value": "",
      "Targets": [
        "name=email",
        "xpath=/html/body/div[4]/div/div/div/form/div[3]/div/div/input",
        "xpath=//input[@name='email']",
        "xpath=//input",
        "css=body > div.pm-module.pm-module--petra > div > div > div > form > div.form__fields.space-y-4 > div:nth-child(1) > div > input"
      ],
      "Description": ""
    },
    {
      "Command": "type",
      "Target": "name=email",
      "Value": "ilovespacerocks@yahoo.com",
      "Targets": [
        "name=email",
        "xpath=/html/body/div[4]/div/div/div/form/div[3]/div/div/input",
        "xpath=//input[@name='email']",
        "xpath=//input",
        "css=body > div.pm-module.pm-module--petra > div > div > div > form > div.form__fields.space-y-4 > div:nth-child(1) > div > input"
      ],
      "Description": ""
    },
    {
      "Command": "click",
      "Target": "name=new_field_2",
      "Value": "",
      "Targets": [
        "name=new_field_2",
        "xpath=/html/body/div[4]/div/div/div/form/div[3]/div[2]/div/input",
        "xpath=//input[@name='new_field_2']",
        "xpath=//div[2]/div/input",
        "css=body > div.pm-module.pm-module--petra > div > div > div > form > div.form__fields.space-y-4 > div:nth-child(2) > div > input"
      ],
      "Description": ""
    },
    {
      "Command": "type",
      "Target": "name=new_field_2",
      "Value": "greafra",
      "Targets": [
        "name=new_field_2",
        "xpath=/html/body/div[4]/div/div/div/form/div[3]/div[2]/div/input",
        "xpath=//input[@name='new_field_2']",
        "xpath=//div[2]/div/input",
        "css=body > div.pm-module.pm-module--petra > div > div > div > form > div.form__fields.space-y-4 > div:nth-child(2) > div > input"
      ],
      "Description": ""
    },
    {
      "Command": "click",
      "Target": "name=new_field_3",
      "Value": "",
      "Targets": [
        "name=new_field_3",
        "xpath=/html/body/div[4]/div/div/div/form/div[3]/div[3]/div/input",
        "xpath=//input[@name='new_field_3']",
        "xpath=//div[3]/div/input",
        "css=body > div.pm-module.pm-module--petra > div > div > div > form > div.form__fields.space-y-4 > div:nth-child(3) > div > input"
      ],
      "Description": ""
    },
    {
      "Command": "type",
      "Target": "name=new_field_3",
      "Value": "gafafdsad",
      "Targets": [
        "name=new_field_3",
        "xpath=/html/body/div[4]/div/div/div/form/div[3]/div[3]/div/input",
        "xpath=//input[@name='new_field_3']",
        "xpath=//div[3]/div/input",
        "css=body > div.pm-module.pm-module--petra > div > div > div > form > div.form__fields.space-y-4 > div:nth-child(3) > div > input"
      ],
      "Description": ""
    },
    {
      "Command": "click",
      "Target": "xpath=/html/body/div[4]/div/div/div/form/div[3]/div[4]/button",
      "Value": "",
      "Targets": [
        "xpath=/html/body/div[4]/div/div/div/form/div[3]/div[4]/button",
        "xpath=//button[@type='submit']",
        "xpath=//button",
        "css=body > div.pm-module.pm-module--petra > div > div > div > form > div.form__fields.space-y-4 > div:nth-child(4) > button"
      ],
      "Description": ""
    },
    {
      "Command": "visualVerify",
      "Target": "yj0ss_96.png",
      "Value": "sub1",
      "Description": ""
    },
    {
      "Command": "else",
      "Target": "",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "gotoLabel",
      "Target": "p1",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    }
  ]
}

Thanks for any help.

If I understand correctly, I’m showing how to do a selectwindow loop and maybe you can try to merge it to your macro.

{
  "Name": "test",
  "CreationDate": "2023-4-17",
  "Commands": [
    {
      "Command": "store",
      "Target": "1",
      "Value": "num",
      "Description": ""
    },
    {
      "Command": "while",
      "Target": "${num} <= 10",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "selectWindow",
      "Target": "tab=${num}",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "executeScript",
      "Target": "return Number(${num}) + 1",
      "Value": "num",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    }
  ]
}

Thanks for your replay. I tried it, but the loop has an end, and I have an if statement in my script as well.

I’m not sure if I understand. You want to

  1. find the form you want to fill in 10 tabs.
  2. fill the form.

Correct?

Then maybe what you need to do is

  1. use the while loop I provided before to create a macro to find the correct form in 10 tabs.
  2. create a macro to fill the form.
  3. embed step 2 to step 1.

A simple reference:

{
  "Name": "test",
  "CreationDate": "2023-4-17",
  "Commands": [
    {
      "Command": "store",
      "Target": "1",
      "Value": "num",
      "Description": ""
    },
    {
      "Command": "while",
      "Target": "${num} <= 10",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "selectWindow",
      "Target": "tab=${num}",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "store",
      "Target": "2",
      "Value": "!TIMEOUT_WAIT",
      "Description": "decrese the searching time"
    },
    {
      "Command": "visualSearch",
      "Target": "t1d8gp_dpi_96.png",
      "Value": "x",
      "Description": ""
    },
    {
      "Command": "store",
      "Target": "30",
      "Value": "!TIMEOUT_WAIT",
      "Description": "change it back to normal"
    },
    {
      "Command": "comment",
      "Target": "do the verify",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "if",
      "Target": "${x}!==0",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "comment",
      "Target": "fill the form",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "break",
      "Target": "",
      "Value": "",
      "Description": "break out of the while loop after finishing"
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    },
    {
      "Command": "executeScript",
      "Target": "return Number(${num}) + 1",
      "Value": "num",
      "Description": ""
    },
    {
      "Command": "end",
      "Target": "",
      "Value": "",
      "Description": ""
    }
  ]
}
1 Like

It actually worked. It didn’t work first, buy I did some changes to my own macro and it worked. Your script worked flawlesly. Thank you very much!

How could I implement the same script multiple times? For example: I have 20 open tabs, and I have 3 forms to fill, how can I ensure the script continues to search and fill the other tabs once it fills the first one?

Thanks a lot.

If you are filling the same content, you can just remove the break statement so it doesn’t break the loop after finishing. And change the condition of the while loop to <=20. By doing that, the macro will loop 20 tabs and fill every form when the IF condition is true.

But if you want to fill 3 different type of form, the simple way I can think of is to create 3 While loop in one macro. Each While loop should fill one type of form.