Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm trying to retrieve some information from Gmail but have been unsuccessful after many attempts. This is the line of code that I'm trying to extract using javascript.

<a href="https://mail.google.com/mail/u/0/#inbox" target="_top" class="J-Ke n0" title="Inbox (182)" tabindex="0">Inbox (182)</a>

Im trying to get the text "Inbox (182)," to do that, I'm using this piece of code

    NSString *js_result = [webview1 stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('J-Ke n0').innerText"];

This however does not work, my result being nothing at all, and I've tried many alternatives but none have worked. All I need to do here is extract the "Inbox (182)" text in any way possible. Thanks.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

I think your javascript is incorrect, since there are multiple elements with that class. If I login to gmail, this works:

document.getElementsByClassName('J-Ke n0')[0].innerText

I would be weary of using this in a production environment, though. It seems very brittle; that class or order of elements could be changed by Google at any time.

You also need to make sure that the page has loaded before trying to execute javascript. Typically this is implemented in a webViewDidFinishLoad: callback. If you're not getting a result and your JS is valid, this is probably the issue.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.