5

I have data

<input name="authenticity_token" type="hidden" value="aiUlw1Yh4W47lPQearSEdTkU0rhKpziZOweq5PMTV0Q=" />

I am trying to select it with Jsoup.

 Element input = doc.select("input[name=authenticity_token]").first();

 String auth_token = input.attr("value");

But this not working.

I am recieving a null pointer exception at String auth_token =.

java.lang.NullPointerException at crime.ic.Main.main(Main.java:2)

What am I doing wrong?

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
woahguy
  • 261
  • 3
  • 13
  • Still not working.. edited previous. – woahguy Dec 16 '15 at 00:12
  • 3
    Are you sure that `doc` contains elements you want to search? Maybe they are generated by JavaScript in which case they are not part of recieved/parsed HTML. – Pshemo Dec 16 '15 at 00:43
  • 2
    Can you add to your post the url accessed in the code? – Stephan Dec 16 '15 at 09:16
  • I have the same problem: I can see the value in IE dev panel but not in the Document retrieved. Other attributes are shown but only the `value` is not. I can see the value in web page. – WesternGun Jul 04 '17 at 12:06

2 Answers2

1

After some researching, I think I know what happens here: the content you want to load may come from some JavaScript and is not visible/existent upon page loading. The loading of JS happens afterwards and fills data in place, and this is not Jsoup can do. So it is beyond reach of Jsoup.

What we need is simulate/automated the loading of JS. Such tools exist.

I found this answer and this question helpful.

WesternGun
  • 11,303
  • 6
  • 88
  • 157
0

If you check this demo you see that there is no problem at all with this case an the input is selected.

I believe that there may be another input with the same name that does not have a value. If that is not the case, post the issue on the JSoup github page.

enrico.bacis
  • 30,497
  • 10
  • 86
  • 115