Continue flow after empty response from HTML node

#1

I'm using html node to apply a CSS selector and sometimes this selector returns nothing, so html node doesn't return anything. Is there a good way to continue the flow anyway and put something like "false" in the payload? Preferrably without using timeouts. Thanks.

0 Likes

#2

I have not used that node, but looking at the readme it seems that if you select the array output then it aught to send an empty array if there are no matches, but that seems not to be the case. I would say that is a deficiency in the node, it might be worth raising an issue against it.
It also doesn't appear to generate anything in a Status node or a Catch node.

0 Likes

#3

In the meantime you could try something like this. You cannot (I think) avoid a timeout as it will have to wait to see if there is a result before taking the action. Probably it could be much less than 250ms. In the Trigger node set the Then Send message to some html that the the html node will parse and pass on something that you can then test to see if it is the 'no result' indication.

[{"id":"b5e86695.593dd8","type":"debug","z":"514a90a5.c7bae8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":579.5,"y":354,"wires":[]},{"id":"81f39430.e6ec3","type":"html","z":"514a90a5.c7bae8","name":"","property":"payload","outproperty":"payload","tag":"","ret":"html","as":"multi","x":457.5,"y":431,"wires":[["b5e86695.593dd8","4548aa4c.7523a4"]]},{"id":"7db97705.1a1bb8","type":"inject","z":"514a90a5.c7bae8","name":"","topic":"","payload":"some html","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":105.5,"y":429,"wires":[["567dfc23.1ad21c"]]},{"id":"567dfc23.1ad21c","type":"trigger","z":"514a90a5.c7bae8","op1":"","op2":"An html string that will give the o/p you want","op1type":"pay","op2type":"str","duration":"250","extend":false,"units":"ms","reset":"","bytopic":"all","name":"","x":279.5,"y":430,"wires":[["81f39430.e6ec3"]]},{"id":"4548aa4c.7523a4","type":"change","z":"514a90a5.c7bae8","name":"","rules":[{"t":"set","p":"reset","pt":"msg","to":"","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":405.5,"y":519,"wires":[["567dfc23.1ad21c"]]}]
0 Likes

#4

Or I suppose that before passing your html to the node you could prepend a bit of html that you know will match, and then ignore the first element in the result. That may be a simpler solution in fact.

0 Likes

#5

@Colin - a simple test would seem to show that if you ask for an array of results, and there is no match then indeed you should get an empty array...

[{"id":"87497856.9d39f8","type":"html","z":"9da3d17d.f408","name":"","property":"payload","outproperty":"payload","tag":"h1","ret":"html","as":"single","x":270,"y":680,"wires":[["61b04dd2.198e94"]]},{"id":"f2062e0.e72a1d","type":"inject","z":"9da3d17d.f408","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"hide":false,"x":140,"y":620,"wires":[["862df4dd.b9ad88"]]},{"id":"862df4dd.b9ad88","type":"function","z":"9da3d17d.f408","name":"","func":"msg.payload = \"<html><body><p>Hello World</p></body></html>\"\nreturn msg;","outputs":1,"noerr":0,"x":300,"y":620,"wires":[["87497856.9d39f8"]]},{"id":"61b04dd2.198e94","type":"debug","z":"9da3d17d.f408","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":680,"wires":[]}]

so if this isn't the case then can we see the details of the flow to check ?

0 Likes

#6

Here is another example on what dceejay suggested

[{"id":"fe068816.7bb6f8","type":"tab","label":"HTML selector","disabled":false,"info":""},{"id":"7d11437a.bb6f5c","type":"inject","z":"fe068816.7bb6f8","name":"Go","topic":"","payload":"Go","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":90,"y":160,"wires":[["5742f4b2.4ec27c"]]},{"id":"a3c9bfc2.02a27","type":"html","z":"fe068816.7bb6f8","name":"Select ","property":"payload","outproperty":"version","tag":"","ret":"text","as":"single","x":550,"y":180,"wires":[["4521a7e3.8680a8"]]},{"id":"1f91ed83.736b02","type":"http request","z":"fe068816.7bb6f8","name":"Endpoint","method":"GET","ret":"txt","url":"https://nodered.org","tls":"","proxy":"","x":420,"y":180,"wires":[["a3c9bfc2.02a27"]]},{"id":"4128fc36.94f324","type":"debug","z":"fe068816.7bb6f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"version","targetType":"msg","x":810,"y":160,"wires":[]},{"id":"8c22c116.9ec0b","type":"debug","z":"fe068816.7bb6f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"select","targetType":"msg","x":310,"y":100,"wires":[]},{"id":"5742f4b2.4ec27c","type":"change","z":"fe068816.7bb6f8","name":"Valid selector","rules":[{"t":"set","p":"select","pt":"msg","to":".node-red-latest-version","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":230,"y":160,"wires":[["1f91ed83.736b02","8c22c116.9ec0b"]]},{"id":"48ced238.6c645c","type":"inject","z":"fe068816.7bb6f8","name":"Go","topic":"","payload":"Go","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":"","x":90,"y":220,"wires":[["ef26470e.0a9278"]]},{"id":"ef26470e.0a9278","type":"change","z":"fe068816.7bb6f8","name":"Invalid selector","rules":[{"t":"set","p":"select","pt":"msg","to":".node-red-latest-versio","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":240,"y":220,"wires":[["1f91ed83.736b02","f56db6e.fd1ba48"]]},{"id":"f56db6e.fd1ba48","type":"debug","z":"fe068816.7bb6f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"select","targetType":"msg","x":310,"y":280,"wires":[]},{"id":"4521a7e3.8680a8","type":"switch","z":"fe068816.7bb6f8","name":"","property":"version","propertyType":"msg","rules":[{"t":"nempty"},{"t":"empty"}],"checkall":"true","repair":false,"outputs":2,"x":670,"y":180,"wires":[["4128fc36.94f324"],["a6f3dd93.fa76b"]]},{"id":"a6f3dd93.fa76b","type":"debug","z":"fe068816.7bb6f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"version","targetType":"msg","x":810,"y":220,"wires":[]}]

Before I tried another approach, that was to check if the selector was included as a substring of the HTML response but came to the conclusion that it would be a bad idea.

0 Likes

#7

Doh! I went back and checked my test flow again, and the selection of array mode had not 'taken'. I guess I must have moved the mouse as I selected it, or something similar, so it was still operating in multi message mode.
So @kuzmich the solution is what I had initially hoped it would be, which is to select array mode, then you can check for an empty array which indicates no match.

0 Likes

#8

Thanks everyone, using an array does indeed solve the problem.

0 Likes