How to ignore <br> and work with commas using storeText?

Hi all,

So, I am trying to get this description on this page: http://www.acailangola.com/saude/PT/products/maquina-anestesia-wato-ex-20-pequena-cirurgia=002

Problem is, it get’s the description in multiple lines and I want it all in one line:

How can I get that in a single line by ignoring
HTML tag?

Now the second problem I will have after this one is solved, is working with commas in a CSV file. I mean, if I want then to separate a file in different columns and save it as a normal Excel file to import on a software, how will I be able to do it?

Thanks in advance. :slight_smile:

The only solution you can do is store the full text and after cut it in some parts with split function (javascript) and save single parts in csv.

The text is a single xpath and will be extracted in an unique element, need cut text extracted with split function.

1 Like

Awesome man, thanks for your help! :smiley:

Although now I have a new problem. I’ve done this and it works like a charm:

 {
  "Name": "Save prods",
  "CreationDate": "2020-9-13",
  "Commands": [
{
  "Command": "storeText",
  "Target": "css=.breadcrumbs a:nth-child(2)",
  "Value": "!csvLine"
},
{
  "Command": "storeText",
  "Target": "css=.breadcrumbs a:nth-child(3)",
  "Value": "!csvLine"
},
{
  "Command": "storeTitle",
  "Target": "xpath=//*[@id=\"left-column\"]/h1",
  "Value": "!csvLine"
},
{
  "Command": "storeAttribute",
  "Target": "css=.img-container>img@src",
  "Value": "!csvLine"
},
{
  "Command": "storeText",
  "Target": "id=product-cod-des",
  "Value": "!csvLine"
},
{
  "Command": "storeText",
  "Target": "xpath=//*[@id=\"details\"]/p",
  "Value": "desc"
},
{
  "Command": "executeScript_Sandbox",
  "Target": "return ${desc}.split(\"\\n\")[0].trim();",
  "Value": "line0"
},
{
  "Command": "executeScript_Sandbox",
  "Target": "return ${desc}.split(\"\\n\")[1].trim();",
  "Value": "line1"
},
{
  "Command": "executeScript_Sandbox",
  "Target": "return ${desc}.split(\"\\n\")[2].trim();",
  "Value": "line2"
},
{
  "Command": "store",
  "Target": "${line0}|${line1}|${line2}",
  "Value": "!csvLine"
},
{
  "Command": "csvSave",
  "Target": "prods.csv",
  "Value": ""
}
  ]
}

In this case, since I was only testing out, I’ve inserted only 3 lines (0, 1 and 2) but then I changed this to consider the possibility of having as much as 20 lines.

Problem is, when it only has, let’s say, 8 lines, it stops there because next execution on line 9 gives this error message:
[error] Line 16: Error in executeScript_Sandbox code: Cannot read property ‘trim’ of undefined

So, probably the best way was to create a loop to first check how much lines there was and then some foreach to work on those number of lines and store it in an array or something like that.

Any suggestions on how I could achieve this?

I usually at the start of macro code I add !errorignore true in this mode macro continue in case of errors.

Another solution is verify variable before save in csv with an if_v2 function but require a long code to verify every variable before save in csv.

{
“Command”: “store”,
“Target”: “true”,
“Value”: “!errorignore”
},

1 Like

Thanks for your help, I was actually forgetting about that which I had already used before in other cases. :slight_smile:

I have this working now and I will mark it as solved, but it would be interesting to have a loop instead of repeating this in 30 or 40 lines.

For future reference, this is the working code (clearly not optimized at all):

{
  "Name": "Save Prods",
  "CreationDate": "2020-9-14",
  "Commands": [
    {
      "Command": "store",
      "Target": "true",
      "Value": "!errorignore"
    },
    {
      "Command": "storeText",
      "Target": "css=.breadcrumbs a:nth-child(2)",
      "Value": "!csvLine"
    },
    {
      "Command": "storeText",
      "Target": "css=.breadcrumbs a:nth-child(3)",
      "Value": "!csvLine"
    },
    {
      "Command": "storeText",
      "Target": "css=.breadcrumbs a:nth-child(4)",
      "Value": "!csvLine"
    },
    {
      "Command": "storeTitle",
      "Target": "xpath=//*[@id=\"left-column\"]/h1",
      "Value": "!csvLine"
    },
    {
      "Command": "storeAttribute",
      "Target": "css=.img-container>img@src",
      "Value": "!csvLine"
    },
    {
      "Command": "storeText",
      "Target": "id=product-cod-des",
      "Value": "!csvLine"
    },
    {
      "Command": "storeText",
      "Target": "xpath=//*[@id=\"details\"]/p",
      "Value": "desc"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${desc}.split(\"\\n\")[0].trim();",
      "Value": "!csvLine"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${desc}.split(\"\\n\")[1].trim();",
      "Value": "!csvLine"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${desc}.split(\"\\n\")[2].trim();",
      "Value": "!csvLine"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${desc}.split(\"\\n\")[3].trim();",
      "Value": "!csvLine"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${desc}.split(\"\\n\")[4].trim();",
      "Value": "!csvLine"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${desc}.split(\"\\n\")[5].trim();",
      "Value": "!csvLine"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${desc}.split(\"\\n\")[6].trim();",
      "Value": "!csvLine"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${desc}.split(\"\\n\")[7].trim();",
      "Value": "!csvLine"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${desc}.split(\"\\n\")[8].trim();",
      "Value": "!csvLine"
    },
    {
      "Command": "executeScript_Sandbox",
      "Target": "return ${desc}.split(\"\\n\")[9].trim();",
      "Value": "!csvLine"
    },
    {
      "Command": "csvSave",
      "Target": "prods.csv",
      "Value": ""
    }
  ]
}