storeText does not work with newline and multi levels


#1

Hi,

On the website https://www.steelmint.com/crc-prices-global 2 I am unable to select the text “FOB Black Sea”.
The xpath is //*[@id=“tblPrices_E_I_”]/tbody/tr[1]/td[1]
But this xpath contains a span and div elements and displays the value of the div element (“W”)

Similarly, on another website I am unable to select text as it contains newline characters.
Attached is the json for this job

{
  "CreationDate": "2018-11-6",
  "Commands": [
    {
      "Command": "open",
      "Target": "http://commerce-app.gov.in/meidb/comq.asp?ie=e",
      "Value": ""
    },
    {
      "Command": "click",
      "Target": "//*[@id=\"radio1\"]",
      "Value": ""
    },
    {
      "Command": "type",
      "Target": "/html/body/form/table[1]/tbody/tr[3]/td[2]/p/input",
      "Value": "7207"
    },
    {
      "Command": "click",
      "Target": "//*[@id=\"radioval\"]",
      "Value": ""
    },
    {
      "Command": "click",
      "Target": "//*[@id=\"button1\"]",
      "Value": ""
    },
    {
      "Command": "storeText",
      "Target": "/html/body/table/tbody/tr[2]/td[3]/font",
      "Value": "myval"
    },
    {
      "Command": "echo",
      "Target": "The value is ${myval}",
      "Value": "green"
    }
  ]
}

#2

Please use tag <code>.
Try copy paste your code without it to get the idea.


#3

Thanks for using the code tag. But note that you must use the code tag directly after you pasted the code in - before hitting “reply”. Because ones you save the post without the code tag, the pasted code gets messed up. For example "..." are converted to “...” so the code snippet can no longer be simply copy & pasted to the Kantu source code tab.

code%20tag


#4

I think I have manged to get the code formatting correct this time. I tried running it again to be extra sure.
On both the websites we face the same issue. Chrome developer tool shows the text being correctly selected by the xpath but we are unable to get the text into a variable.


#5

I confirmed the extract problem for this specific “FOB Black Sea” element and created a ticket for it. As a workaround, maybe use sourceExtract.


#6

Thanks for the input. Unfortunately sourceExtract will not work in my case. I need a method to be able to extract all elements of the table. I was hoping that something along the lines of
storeEval | x = document.getElementById("/html/body/table/tbody/tr[2]/td[3]").text() | myval
I have tried the storeEval for the second issue (source json is in the message). The above does not work, but is something along the same lines possible? It would be of great help.


#7

why sourceExtract doesn’t work in your case?


#8

I was able to get sourceExtract to work. This is with regards to the above pasted code. The variable now contains the desired value along with extra characters that need to be removed. I am unable to remove the extra characters due to the presence of "\n". I have found this related to the following post
storeEval Javascript [error] Error in runEval code: Invalid or unexpected token

sourceExtract | VERTICAL-ALIGNfont size\n*@3 | rawName
rawname contains “<td style=“VERTICAL-ALIGN: top”><font size=“2”>\n SEMI-FINISHED PRODUCTS OF IRON OR NON- ALLOY STEEL”


#9

I don’t understand you, whether it works for you or not.
But by the context my guess is you have to search online for “regex lookaround”


#10

Many thanks!! I did not know about regex lookaround (Lookahead and Lookbehind). With your help I was able to write a correct regex to filter and get only the desired part of the string.
Attaching the working and corrected code for reference of others like me.

{
  "CreationDate": "2018-11-11",
  "Commands": [
    {
      "Command": "open",
      "Target": "http://commerce-app.gov.in/meidb/comq.asp?ie=e",
      "Value": ""
    },
    {
      "Command": "click",
      "Target": "//*[@id=\"radio1\"]",
      "Value": ""
    },
    {
      "Command": "type",
      "Target": "/html/body/form/table[1]/tbody/tr[3]/td[2]/p/input",
      "Value": "7203"
    },
    {
      "Command": "click",
      "Target": "//*[@id=\"radioval\"]",
      "Value": ""
    },
    {
      "Command": "click",
      "Target": "//*[@id=\"button1\"]",
      "Value": ""
    },
    {
      "Command": "sourceExtract",
      "Target": "regex=(?<=VERTICAL-ALIGN.*font size.*[\\n\\s \\t]+).*@3",
      "Value": "val_1"
    },
    {
      "Command": "echo",
      "Target": "${val_1}",
      "Value": "blue"
    },
    {
      "Command": "storeEval",
      "Target": "x=\"${val_1}\"; x.replace(/ [\\t ]/g,\"\"); ",
      "Value": "val_2"
    },
    {
      "Command": "echo",
      "Target": "${val_2}",
      "Value": "green"
    }
  ]
}