1

I'm trying to get clickable elements from a UIWebView based on coordinates ("What is at coordinate x,y?"), retrieve information about them ("Is it a link? Where does it go?"), and then programmatically click them, and I've run across a couple of problems.

elementFromPoint(x,y) works wonderfully in terms of getting the elements at a given location, but if I have, for example, a <span> inside an <a> then the span is reported instead of the <a>. Is there any way to get around this? Or to go to the parent above this element, but at the same coordinates, if it's not clickable?

Also, click() works great for buttons, but not for links. How would I simulate a mouse click (or a tap) on a link, or a mouseover? I tried onmouseover() on the elements, but that didn't work.

Alternatively, is there any kind of mouse object on the page that I could programmatically move around and that would do what I want?

2 Answers 2

2

So what I ended up doing for the "mouseover" part of this was just checking if the given element is an anchor or a form object, and if not I go to the parentNode and check that, and I do this for up 2 parents before giving up. That seems to be good enough for just getting the links themselves.

For clicking an element it's even easier as I don't have to traverse up the hierarchy. I just create the mouse click event and pass it to the element detected in elementFromPoint(), and the page will automatically forward this up the hierarchy itself.

[webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"var theEvent = document.createEvent('MouseEvent');\n theEvent.initMouseEvent('click', true, true, window);\n awElement.dispatchEvent(theEvent);\n awElement.click();\n", mouseX, mouseY]];

So easy! Just wish I could do the same to get the link for the element.

Sign up to request clarification or add additional context in comments.

Comments

0

If my understanding is correct.

Try "document.location" and set the value string u need to navigate to.

UIWebViewDelegate function - shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType { will be called and request URL will give you it.

1 Comment

That's not what I'm trying to do. When I'm talking about location I mean in coordinates on the given page. the document.location refers to the current document, not a location on the document. At least as far as I can tell.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.