0

On a web page in my web browser (preferably, but not necessarily Firefox), I can search (byctrl+f) for a given text "abc" within the body text of the page. From there then I must move the mouse cursor to another (relative) position (height plus x pixels), and there I must do a mouse click.

I cannot do this otherwise since the needed info is not contained in the source code but is fetched by mouse click from the web server. The problem for me is to identify the position of the found text "abc", in order to move the mouse cursor there; from there it's easy.

I currently try to solve my problem by searching for the background color which changes for the text "abc" when found, but the same color is found in lots of other positions on the screen, so this is unreliable, and finding the text "abc" as a graphic is unreliable, too. So I'm looking for an alternative, programmatic way to identify the position of found text, if there is any.

I work with AutoHotkey, but could use other scripting languages or JavaScript triggered from within AHK.

Rob Lyndon
  • 12,089
  • 5
  • 49
  • 74
MaryL
  • 1
  • "abc" is multiple times within the body text, so I need to treat the different occurrences in a loop. And I need to check and process other things before clicking or not. And I tried to programmatically click the identifiable element (not the "abc"), but while a virtual click does not work (even though it's executed on that element), only a semi-real click, with the mouse cursor at the click position (in AHK "click, x, y"), will work, so I first must move the mouse cursor to "abc", but as said, it's found-selected-text, so I could have worded: "How to identify coordinates of selected-text?" – MaryL Jan 30 '17 at 21:09
  • And so this is a double of http://stackoverflow.com/questions/6846230/coordinates-of-selected-text-in-browser-page Will try the solution given there. – MaryL Jan 30 '17 at 21:25
  • From here http://stackoverflow.com/questions/4163879/call-javascript-function-from-url-address-bar is seems you cannot trigger a JS function from the address line of the browser but you would need to do this triggering a bookmark; I don't see at this time how this would be doable for a multi-executional script. On the other hand, you can put multi-line JS code into the address line, so I'll try that. – MaryL Jan 30 '17 at 21:41
  • I reformatted the code from the other thread with tab indents. Then I deleted all tabs and newlines, I replaced the return line with an alert line and I deleted all comments. Then I selected something in any webpage, then I put the now-1-line-code into the address line, by control-c, then I typed javascript: before and pressed enter. Nothing, not even the alert. So is it not possible to execute a whole function from the address line? Since from the other thread here, this code should work on any webpage: – MaryL Jan 30 '17 at 22:35
  • javascript:win = win || window;var doc = win.document;var sel = doc.selection, range, rects, rect;var x = 0, y = 0;if (sel){if (sel.type != "Control"){range = sel.createRange();range.collapse(true);x = range.boundingLeft;y = range.boundingTop;}}else if (win.getSelection){sel = win.getSelection();if (sel.rangeCount){range = sel.getRangeAt(0).cloneRange();if (range.getClientRects){range.collapse(true);rects = range.getClientRects();if (rects.length > 0){rect = rects[0];}x = rect.left;y = rect.top;}if (x == 0 && y == 0){var span = doc.createElement("span"); – MaryL Jan 30 '17 at 22:36
  • if (span.getClientRects){span.appendChild( doc.createTextNode("\u200b") );range.insertNode(span);rect = span.getClientRects()[0];x = rect.left;y = rect.top;var spanParent = span.parentNode;spanParent.removeChild(span);spanParent.normalize();}}}}alert("x: " + x + ", y: " + y); So why does this not work? – MaryL Jan 30 '17 at 22:37

1 Answers1

0

If your intent is simply to click on the element, it's not even necessary to move the cursor to it, as you can click it directly. Here's a snippet to find the given element and then attempt to click on it, utilising JavaScript and jQuery:

var word = {TARGET WORD},
    queue = [document.body],
    curr;

while (curr = queue.pop()) {
  if (!curr.textContent.match(word)) continue;
  for (var i = 0; i < curr.childNodes.length; ++i) {
    switch (curr.childNodes[i].nodeType) {
      case Node.TEXT_NODE:
        if (curr.childNodes[i].textContent.match(word)) {
          $(curr)[0].click();
        }
        break;
      case Node.ELEMENT_NODE:
        queue.push(curr.childNodes[i]);
        break;
    }
  }
}

Note that {TARGET WORD} must be self-contained within an element, as it si the element that the code will try to click. For example, if the word is within a paragraph, it will attempt to click the entire paragraph.

I've created a fiddle demonstrating this here.

Hope this helps!

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71