Essayer de remplir un formulaire de site Web en utilisant java, mais la balise de formulaire est intégrée à la balise iframe


Mon objectif est d'accéder à cette URL http://eaacorp.com/find-a-dealer et remplissez un formulaire en utilisant java. Pour ce faire, j'ai tenté de trouver toutes les balises de formulaire:

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class HttpUrlConnectionExample{

    public static void main(String[] args) throws IOException{
        Document document = Jsoup.connect("http://eaacorp.com/find-a-dealer").get();
        String page = document.toString();//this is the whole page's html

        Elements formEl = document.getElementsByTag("form");
    }

}

Cependant formEl renvoie vide car la balise de formulaire est incorporée dans le http://www.eaacorp.com/dealer/searchdealer.php html dans la balise iframe (extrait de la source de la page):

<iframe id="blockrandom" name="iframe" src="http://www.eaacorp.com/dealer/searchdealer.php" width="100%" height="500" scrolling="auto" frameborder="1" class="wrapper"></iframe>

Par conséquent, existe-t-il un moyen d'accéder à la balise de formulaire dans la balise iframe? Quelque chose comme:

if(formEl.isEmpty()){
    //find iframe
    Elements iframeEl = document.getElementsByTag("iframe");
    System.out.println(iframeEl);
    String embedURL = iframeEl.getSrc();//DOES NOT COMPILE, getSrc() is not a method
    Document embedDoc = Jsoup.connect(embedURL).get();
}
Author: Travis Ho, 2016-10-08

2 answers

Il n'y a pas besoin de votre propre méthode getSrcString, d'autant plus que l'approche sous-chaîne se cassera pour des changements minimes dans la balise.

Utilisez plutôt .attr("abs:src") sur un élément avec l'attribut src (comparez: https://jsoup.org/cookbook/extracting-data/working-with-urls )

Exemple De Code

Document document = Jsoup.connect("http://eaacorp.com/find-a-dealer").get();
Element iframeEl = document.select("iframe").first();
String embedURL = iframeEl.attr("abs:src");
Document embedDoc = Jsoup.connect(embedURL).get();

System.out.println(embedDoc.select("form").first());

Sortie Tronquée

<form action="findit.php" method="post" name="dlrsrchfrm" target="_blank"> 
    <div style="padding: 15px;">
    [...]
</form> 
 0
Author: Frederic Klein, 2016-10-10 06:27:16

J'ai trouvé que vous pouviez réellement créer votre propre méthode qui peut obtenir l'URL src en utilisant des sous-chaînes, puis simplement utiliser cette chaîne pour obtenir une connexion de document:

public static String getSrcString(String html){
    String construct = "";
    for (int i = 0; i < html.length() - 5;i++){
        if (html.substring(i, i + 5).equals("src=\"")){
            i += 5;
            while(!html.substring(i, i + 1).equals("\"")){
                construct += html.substring(i, i + 1);
                i++;
            }
        }
    }
    return construct;
}

Puis dans le principal:

String embedURL = getSrcString(iframeEl.toString());
Document embedDoc = Jsoup.connect(embedURL).get();
 0
Author: Travis Ho, 2016-10-08 20:11:59