Using an If to check that a string contains a certain substring

I’m new to Kantu and one of the things I’m trying to do with my first macro is click a certain area of the screen based on some text that is already there. I’ve stored the string in a variable (Description) and I now want to check it for a substring using an if statement. I’ve been trying several different ways to use a contains function, but I’m apparently failing to hit the right syntax.

So, what should my condition (Target:) be for the if statement if I want it to evaluate true when the variable includes the number 110?

Here is a code snippet to check that a string contains a certain substring. I copied the code from the DemoPDFTest_with_OCR macro that ships with Kantu.

{
      "Command": "storeEval",
      "Target": "var string = \"${q}\", substring = \"110\";  b= string.includes(substring); b;",
      "Value": "textfound"
    },
2 Likes

Thank you! I went through several of the demos, but I don’t think I went through that one. I appreciate the help!

Update: Since UI.Vision Version 4 and newer, the easier solution is to use executeScript command and the Javascript .includes feature to check that a string contains a certain substring (“string comparison”):

{
  "Name": "includes",
  "CreationDate": "2019-6-24",
  "Commands": [
    {
      "Command": "store",
      "Target": "test tim meyer",
      "Value": "e"
    },
    {
      "Command": "if_v2",
      "Target": "${e}.includes (\"tim\") == true",
      "Value": ""
    },
    {
      "Command": "echo",
      "Target": "yes, it contains this string",
      "Value": "blue"
    },
    {
      "Command": "end",
      "Target": "",
      "Value": ""
    }
  ]
}

this is not working for me. When I set the if_v2 target to:

${!SCRAPE}.includes (“test”) == true

to search the variable SCRAPE for the word “test” I get this:

error]

Line 31: Error in condition of if_v2: Invalid or unexpected token.

What am I doing wrong?

Your personal variables must not include ! - the ! symbol is reserved for the internal variables.

Hello, Happy New Year!
This command works like charm, but when we have a substring with some accent, like “Orange-PP”, the Macro can’t handle due “-” character and simply ignore. How can we fix this? Thanks!

See also Selenium IDE string operations with executeScript_Sandbox - it has examples for removing characters from string.

There is [error][ignored] [Line 34]: undefined is not a function
when I run my code

{
“Name”: “CafeF-UPCoM-BCTC-download-v2”,
“CreationDate”: “2022-10-13”,
“Commands”: [
{
“Command”: “comment”,
“Target”: “if_v2 // ${!LOOP}==1”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “open”,
“Target”: “https://www.hnx.vn/thong-tin-cong-bo-up-tcph.html”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “comment”,
“Target”: “open // chrome://settings/content/pdfDocuments”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “executeScript”,
“Target”: “d=new Date(); d.setDate(d.getDate()-1);\nvar m=((d.getMonth()+1)<10)?‘0’+(d.getMonth()+1):(d.getMonth()+1); var dd=d.getDate(); \nif (dd<10) {dd=‘0’+dd;} return dd+”/"+m+"/"+d.getFullYear();",
“Value”: “d1”,
“Description”: “dd/mm/yyyy”
},
{
“Command”: “comment”,
“Target”: “executeScript // d=new Date();\nvar m=((d.getMonth()+1)<10)?‘0’+(d.getMonth()+1):(d.getMonth()+1); var dd=d.getDate(); \nif (dd<10) {dd=‘0’+dd;} return dd+”/"+m+"/"+d.getFullYear();",
“Value”: “d1”,
“Description”: “dd/mm/yyyy”
},
{
“Command”: “executeScript”,
“Target”: “d=new Date(); var m=((d.getMonth()+1)<10)?‘0’+(d.getMonth()+1):(d.getMonth()+1); var dd=d.getDate(); \nif (dd<10) {dd=‘0’+dd;} return dd+”/"+m+"/"+d.getFullYear();",
“Value”: “d2”,
“Description”: “dd/mm/yyyy”
},
{
“Command”: “executeScript”,
“Target”: “d=new Date(); \nvar m=((d.getMonth()+1)<10)?‘0’+(d.getMonth()+1):(d.getMonth()+1);\nvar dd=d.getDate(); \nvar year = d.getFullYear();year = year.toString().substr(-2);\nif (dd<10) {dd=‘0’+dd;} return year+m+dd;”,
“Value”: “d3”,
“Description”: “dd/mm/yyyy”
},
{
“Command”: “echo”,
“Target”: “${d1}”,
“Value”: “blue”,
“Description”: “”
},
{
“Command”: “echo”,
“Target”: “${d3}”,
“Value”: “blue”,
“Description”: “”
},
{
“Command”: “type”,
“Target”: “id=txtTuNgay”,
“Value”: “${d1}”,
“Description”: “”
},
{
“Command”: “type”,
“Target”: “id=txtDenNgay”,
“Value”: “${d2}”,
“Description”: “”
},
{
“Command”: “store”,
“Target”: “true”,
“Value”: “!errorIgnore”,
“Description”: “”
},
{
“Command”: “click”,
“Target”: “xpath=//[@id=“divSearchContentArticle”]/ul[1]/li[2]/div/div/div[2]/ul/li[12]/label/input",
“Value”: “”,
“Description”: “”
},
{
“Command”: “select”,
“Target”: “id=cboNhomTin”,
“Value”: “value=FIN_REPORT”,
“Description”: “”
},
{
“Command”: “select”,
“Target”: "xpath=//
[@id=“divNumberRecordOnPageTCPHUpCoM”]”,
“Value”: “label=50”,
“Description”: “”
},
{
“Command”: “click”,
“Target”: “XPATH=//[@id=“btn_searchL”]",
“Value”: “”,
“Description”: “”
},
{
“Command”: “pause”,
“Target”: “5000”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “storeXpathCount”,
“Target”: “xpath=//div[2]/div[2]/div/table/tbody/tr/td[3]”,
“Value”: “amount”,
“Description”: “”
},
{
“Command”: “echo”,
“Target”: “${amount}”,
“Value”: “blue”,
“Description”: “”
},
{
“Command”: “times”,
“Target”: “${amount}”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “storeText”,
“Target”: “xpath=//div[2]/div[2]/div/table/tbody/tr[${!times}]/td[3]”,
“Value”: “ticker”,
“Description”: “”
},
{
“Command”: “echo”,
“Target”: “${ticker}”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “storeText”,
“Target”: “xpath=//div[2]/div[2]/div/table/tbody/tr[${!times}]/td[5]”,
“Value”: “title”,
“Description”: “”
},
{
“Command”: “echo”,
“Target”: “${title}”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “store”,
“Target”: “${ticker}”,
“Value”: “!csvLine”,
“Description”: “”
},
{
“Command”: “store”,
“Target”: “${title}”,
“Value”: “!csvLine”,
“Description”: “”
},
{
“Command”: “store”,
“Target”: “${d1}”,
“Value”: “!csvLine”,
“Description”: “”
},
{
“Command”: “csvSave”,
“Target”: “tickerQ3”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “click”,
“Target”: “xpath=/html/body/div[1]/div[2]/div[3]/div[5]/div[2]/div[2]/div[1]/table/tbody/tr[${!times}]/td[5]/a”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “storeText”,
“Target”: "xpath=//
[@id=“divViewDetailArticles”]/div[]/div[3]/p[1]/a",
“Value”: “filename1”,
“Description”: “”
},
{
“Command”: “echo”,
“Target”: “${filename1}”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “store”,
“Target”: “2”,
“Value”: “!TIMEOUT_WAIT”,
“Description”: “”
},
{
“Command”: “onDownload”,
“Target”: “${ticker}_22Q3_BCTC.pdf”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “if_v2”,
“Target”: “${filename1}.includes(“me”)==true”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “onDownload”,
“Target”: “${ticker}_22Q3_BCTC_M.pdf”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “end”,
“Target”: “”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “click”,
“Target”: "xpath=//
[@id=“divViewDetailArticles”]/div[]/div[3]/p[1]/a",
“Value”: “”,
“Description”: “”
},
{
“Command”: “end”,
“Target”: “”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “comment”,
“Target”: “if_v2 // times==${amount}”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “comment”,
“Target”: “localStorageExport // C:\Users\XanhCloud\Desktop\uivision\datasources\tickerQ3.csv”,
“Value”: “#DeleteAfterExport”,
“Description”: “”
},
{
“Command”: “comment”,
“Target”: “XRun // C:\WINDOWS\system32\cmd.exe”,
“Value”: “”,
“Description”: "/c move /Y "C:\Users\Admin\Downloads\
.pdf” “D:\@BCTC-PDF\UPCoM\${d3}”"
},
{
“Command”: “if_v2”,
“Target”: “${amount}>50”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “run”,
“Target”: “CafeF-UPCoM-BCTC-download-page2”,
“Value”: “”,
“Description”: “”
},
{
“Command”: “end”,
“Target”: “”,
“Value”: “”,
“Description”: “”
}
]
}