Applying a single command to multiple targets?


#1

Hello there. I’m trying to find an elegant solution to a problem, but I am new to Kantu and don’t really know what I’m doing.

For context, imagine that you are a professor with a list of assignments and their due dates. I want to be able to change the dropdown date of each and every assignment to the 15th. I thought I was being clever by using the While command and a partial match in the id, BUT it doesn’t work. Whenever the macro loops, it remains on the first assignment, continually changing the date to the 15th.

Does anyone have ideas about how to loop a single command through multiple targets? There will not always be the same number of targets. It will vary.

Here’s what I was trying:

{
“CreationDate”: “2018-10-1”,
“Commands”: [
{
“Command”: “while”,
“Target”: “1000 > 5”,
“Value”: “”
},
{
“Command”: “select”,
“Target”: “//*[contains(@id,“duedate_day”)]”,
“Value”: “label=15”
},
{
“Command”: “endWhile”,
“Target”: “”,
“Value”: “”
}
]
}


#2

Your code almost works. You only need to make sure you the get n-th instance of an element with xpath and not only the 1st. Tthis should work:

  • First instance: //*[contains(@id,“duedate_day”)][1]
  • Second instance: //*[contains(@id,“duedate_day”)][2]
  • Third instance: //*[contains(@id,“duedate_day”)][3]

In general //somexpression[N] means “Find every node selected by //somexpression that is the N th child of its parent”.

So in the while loop you can use

//*[contains(@id,“duedate_day”)][${myIndex}]

and then increase the ${myIndex} with every loop with storeEval

{
  "Command": "storeEval",
  "Target": "${myIndex}+1 ",
  "Value": "myIndex"
},

The storeEval idea is from this post.


#3

Brilliant! Thank you so much. This makes a lot of sense.


#4

Sorry, I do have a follow-up question! The code seems to be having trouble finding the second instance of the expression. There are other unrelated elements on the page between each of the “due date” drop-downs–enough so that you would have to scroll a little if you were doing it manually. Could this cause a problem with xpath finding the instances?

Thanks!