How can I click on a link by using the stringByEvaluatingJavaScriptFromString method in a UIWebView? The actual hyperlink itself is different every time but the wording (“Test”) inside the Test code is always the same.
How would I go about doing this? I have had a suggestion to do the following (which doesn’t work and doesn’t do anything):
var fonts = document.getElementsByTagName('font');
for(i=0;i<fonts.length;i++) {
if (fonts[i].innerHTML == 'Test') {
fonts[i].parentNode.childNodes[0].click();
break;
}
}
Looks like you have an answer including the Objective-C code you need, but I would imagine that for the javascript you can simply fire the contents of the link’s onclick or navigate to the appropriate url instead of going through the trouble of simulating a mouse click. Granted, I have not tested this in an iOS app but feel free to give it a shot:
By the way, there is an eval in this code that will run the contents of the onclick handler. This can be unsafe. If you only need to navigate to a location specified by the link then you may want to remove the portion dealing with onclick.
A much simpler version of this is printed below (since your sample code does not appear to need to deal with onclick) which may be easier to use. In this case I am calling it after a 1-second delay, but in reality, since it seems that this code runs in response to a trigger on the iOS side (button click?), you should not use a delay and instead simply wait enable the trigger until loading is complete –
webViewDidFinishLoad.jsFiddle which demonstrates this using your sample code: http://jsfiddle.net/fzKL7/5/