Search code examples
javaandroidjsoup

How to fix Jsoup query returning null


I'm trying to retrieve a value of an element with the id loggedin from a WebView using Jsoup, i'm pretty sure it exists in the webpage i'm loading but i keep getting null

this is the code:

Document doc = Jsoup.parse(webView.getUrl());
System.out.println("Webview url= "+webView.getUrl());
Elements loggedin = doc.select("#loggedin");
System.out.println("loggedin= "+ loggedin.first());

System.out output:

04-03 18:48:15.511 17236-17236/com.gci.gestioncapteursincendie I/System.out: Webview url= http://gestioncapteursincendie.herokuapp.com/
04-03 18:48:15.521 17236-17236/com.gci.gestioncapteursincendie I/System.out: loggedin= null

Solution

  • The problem is that you use Jsoup.parse instead of Jsoup.connect

    parse used for parsing html page, and connect to load page from specific url

    Just replace #loggedin with input#loggedin

    Example doc.select("input#loggedin");

    Or you can use getElementById to get Element

    doc.getElementById("loggedin");