Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get hidden input value with JSoup?

Tags:

java

jsoup

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?

like image 500
woahguy Avatar asked Nov 09 '22 23:11

woahguy


1 Answers

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.

like image 160
WesternGun Avatar answered Nov 14 '22 21:11

WesternGun