How can I scrape an element that may or may not be present and then skip to the next part of my process?

Let’s say I’m trying to scrape some category labels of different blog posts and save them individually to a CSV line.

Each blog post might have one to five categories stored individually in this element:

//article/a

So, to find each category value I can add increments to this Xpath like this:

//article/a[1]
//article/a[2]
//article/a[3]
//article/a[4]
//article/a[5]

However, if a given blog post only has one category, this means that ui.vision will waste quite a bit of time searching for the other four categories that don’t exist and also return several #LNF values in my CSV file.

Is there a way to tell ui.vision something like:

“If //article/a[2] isn’t found, don’t bother looking for //article/a[3], //article/a[4], etc. Just skip to the next part of the scraping process. Also, don’t log #LNF for any category you couldn’t find.”

I’m guessing there are probably a few ways to do this, so I appreciate any workarounds the community here can offer.

Thanks!

If //article/a[2] isn’t found, then the result will be "#LNF => You can check on this result, and then use IF/THEN to skip the //article/a[3] (etc) searches.

2 Likes

Thanks @ulrich

I tried using GotoIf_V2 to jump to a label outside my ${!times} loop if “#LNF” appears in my csvLine, however it doesn’t seem to be working. Could you kindly offer some guidance on what I’m doing wrong. My steps look like this:

{
  "Command": "times",
  "Target": "5",
  "Value": "",
  "Description": ""
},
{
  "Command": "storeText",
  "Target": "//article/a[${!times}]",
  "Value": "!csvLine",
  "Description": ""
},
{
  "Command": "gotoIf_v2",
  "Target": "${!csvLine} == \"#LNF\"",
  "Value": "HERE",
  "Description": ""
},
{
  "Command": "end",
  "Target": "${!times}",
  "Value": "",
  "Description": ""
},
{
  "Command": "label",
  "Target": "HERE",
  "Value": "",
  "Description": ""
},

Thanks again.