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?
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.
So easy! Just wish I could do the same to get the link for the element.