Can KM find specific text in a webpage?

Hello,

To execute some actions(3) when a webpage(1) shows a specific word(2), I set the "until" macro like below. But the finding image doesn't work always and it often just reloads the URL(1) even when the word(2) appeared on the screen(1). Maybe it's because finding image takes some amount of time?

I captured the specific word on the webpage as an image(2) and put it in the found image condition because I couldn't find the way to get KM to find the word. If finding text is possible and it is faster than finding image, please let me know how to set it.

Thanks,
Kyu

Hey Kyu,

Use a Wait For Google Chrome to Finish Loading action instead of a plain pause.

I don’t see a verb in Chrome’s AppleScript dictionary for getting the page text, but you can do it with JavaScript.

Using an Execute an AppleScript action:

------------------------------------------------------------
set javascriptStr to "document.body.parentNode.outerText"
tell application "Google Chrome"
  tell front window
    tell active tab
      set chromeFrontPageText to execute javascript javascriptStr
    end tell
  end tell
end tell
------------------------------------------------------------

Or try the bare JavaScript in an Execute a JavaScript in Google Chrome action.

Beware – all of Chrome’s actions are run via AppleScript, and sometimes Chrome’s AppleScript support breaks – requiring a restart of Chrome.

I doubt there will be any difference in the reliability of either of these actions, so the bare JavaScript will be cleaner.

Route the output of the action to a Keyboard Maestro variable.

You have a number of options for finding your word or phrase:

  • AppleScript
  • JavaScript for Automation
  • Keyboard Maestro Search action

That should get you started.

-Chris

Thank you for your kind help Chris. But unfortunately, since I’m just a beginner, I hardly understood your answer regarding JavaScript. So could you possibly let me know how to set it for the action with one of the options you suggested?

Chrome or Safari doessn’t matter, I need KM to refresh a certain webpage until a specific word appears on the page. And when the word appeared, click a link on the same page immediately.

I know this request might be too much for me to ask you. If you’re not able to do this for me, please just let me know where I can do some research or study about JavaScript and KM variable system.

Thanks a lot
Kyu

Hey Kyu,

Okay, here's a working example.

(It will only work as is while “Smart Converter Pro 2.2.1” is on MacUpdate, after that the text and the link would need to be altered.)

-Chris


Generic-Test 01.kmmacros (4.1 KB)

2 Likes

It works like magic! :heart_eyes:

Beware – all of Chrome's actions are run via AppleScript, and sometimes Chrome's AppleScript support breaks – requiring a restart of Chrome.

But one more question please, is this why you made the macro with Safari? Don't I need to worry about the problem when I use Safari instead of Chrome?

Thank you very, very much, Chris. :+1: :sparkles:

Hi Chris...
So is this a custom string you created or is it sort of a standard/common string?
John

Hey John,

That's “simple” JavaScript that talks to the DOM of the webpage. (Document Object Model)

I'm just learning JavaScript, so I don't know all the ins and outs as yet.

Web pages can be very complicated with frames and all kinds of goodies.

I picked up this bit of code from some folks I know who use it all day long in one of their processes, and it's the most bombproof “address” for extracting text from a webpage I've found so far.

-Chris

2 Likes

Thanks, I’ll have to look into it further.

I have a similar issue. On online DB I paste in word. Hit return and up comes the page I was after. But then I need to click on the word I did a search on that will be somewhere on the page. I can do an ordinary browser search and highlight the word. But what I need to do is click the searched word which will now take me to another page from whence I can continue digging.

If I can do a search on the highlight colour only, then I could click on found image but most of it is the text of the word itself.

Any ideas?

Cheers

Michael

I'm not sure I understand your workflow. Does Word need to be involved?
OR, do you need to search for a word on the browser web page, and then click that link?

If you could provide an example web page and your workflow, it would be very helpful.

Managed to sort using found image.

But what I was referring to was doing a search for a word on a webpage which when found gets highlighted. Then using the actual “highlighting” for click purposes.

Still posting the question probably helped me step back and use a different image and just offset the click.

Thanks for responding.

Michael

If you are satisfied that you have a good solution, then that's great!

I'll just mention two things, for you and all readers:

  1. Searching for Found Image (KM Wiki) can be trickly, and is not as reliable as many other searches. For me, it is the method of last resort.
  • It would not be too hard to write a JavaScript that would:
    • Search all links on a page for specific text in the link text
    • Allow you to pick one (or more), and then use the link for that text to open another web page

Good luck!

Yes agree. JavaScript would be a better solution but do not have the skills.

However am still getting my head round KM :slight_smile:

Thanks

Michael

If you want an example for how to find text on a web page (without Javascript) you can have a look at my Get Fragment URL from Safari macro. Maybe you can adapt it to your needs.

1 Like

I got it to work, but there are other things that are extremely unwieldy, as the web interface is all Javascript driven and there are not “real” Chrome fields or buttons. I am going to have to do all Finds and Image clicks.

You might still be able to use JavaScript in Browser to access these fields. They are likely real fields, just dynamically produced by JavaScript.

IAC, post a URL if you'd like more help.