Search web page for text (If Then Else)

I'm editing thousands of product ID's on a website (each ID being its own webpage) and sometimes the site will error, halting the macro. The simple fix is to go back a page and then restart the macro.

I'd like to add a check at the top of my macro that looks for specific text (i.e. "WEBSITE ERROR") and performs an alternate action of going back a page. This would be nested inside of a loop.

The loop does a check to see if the page error'd. If it did, it will go back a page. If it didn't, it will run the macro to edit the page. Repeat.

I'm just not sure how to check the web page for text and use it in an If/Then action. Any help would be much appreciated.

Please upload your macro so we can understand and test.
Also, please provide:

  • Screenshot that shows the "WEBSITE ERROR"
  • URL of page, or HTML code of page

Finally, Please read:
Tip: How Do I Get The Best Answer in the Shortest Time?

Thanks for the help in exporting and uploading the macros and screenshots. Good to know for future reference.

There are 3 macros/screenshots:

  • _Run this macro (the master macro I run that runs 2 macros in succession)
  • Counter (step 1 of the master macro)
  • Color Number (step 2 of the master macro)

The counter simply loads the next web page (product ID) by editing the current URL, copying the product ID within the URL (i.e. 20,258), adding 1 (making the new URL 20,259), and pasting the new ID over the old.

When the web page loads, Color Number is run. This just edits a field on the web page. After the field is edited, the cycle repeats itself (i.e. load next page, edit page, load next page, edit page, etc.)

I can't provide the website without getting into trouble and it's locked behind a login/password anyway.

I'd like to add a Check at the beginning of the master macro to make sure that the web page hasn't error'd. While errors are rare, they do happen and that halts the macro. I was hoping an If/Then action would suffice at the top of the master macro. If the page error'd, go back one page (command + left arrow). Else, run Counter followed by Color Number.

Hopefully that makes sense. In addition to the macros and screenshots, I've included a screenshot of the error that pops up. I'm thinking the Check could search the web page for "Application Error" in order to see the web page error'd or not.

_Run This Macro.kmmacros (2.5 KB) Counter.kmmacros (6.4 KB) Field: Color Number.kmmacros (7.1 KB)

Maybe something like this will work.
However, instead of testing for the error text on the page, what about testing for the page URL? When you get an error, is there always at least part of the URL that is the same?
If so, that would be much faster than getting and testing the text contents of every page.

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.


MACRO:   Check for Specific Text on Web Page @TEST

-~~~ VER: 1.0    2020-05-21 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Check for Specific Text on Web Page @TEST.kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.

Unfortunately, I believe the URL remains the same when it error's. Thank you very much! I'll give this a go and see how it performs. I'll report back tomorrow.

That would be very unusual behavior, particularly since the error page says "your page can't be served". It's worth double-checking.

I have yet to see an error page...unsurprising when it's the one thing I want to see. I'm going to mark your post as the solution as I did test it out to make sure it works with other text.

Thank you much, JMichael.

1 Like