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.
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:
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.
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.
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.
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.
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.
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.
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.