2016-10-08 4 views
1

にフォームタグが埋め込まれています。私の目標はこのURL http://eaacorp.com/find-a-dealerにアクセスし、javaを使用してフォームに記入することです。JavaScriptを使用してウェブサイトのフォームに記入しようとしましたが、iframeタグ

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

:formタグがiframeタグ(ページのソースの抜粋)にhttp://www.eaacorp.com/dealer/searchdealer.php HTMLに埋め込まれているため

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"); 
    } 

} 

しかしformElは空を返します。これを行うには、私はすべてのフォームタグを見つけることを試みしたがって、iframeタグ内のformタグにアクセスする方法はありますか?ような何か:

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(); 
} 

答えて

0

サブストリングアプローチはタグで最小限の変更のために中断します、特に以来、独自のgetSrcString方法のための必要はありません。

代わりsrc属性を持つ要素の使用.attr("abs:src")(比較:https://jsoup.org/cookbook/extracting-data/working-with-urls

サンプルコード

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()); 

切り捨てられた出力

<form action="findit.php" method="post" name="dlrsrchfrm" target="_blank"> 
    <div style="padding: 15px;"> 
    [...] 
</form> 
0

私はあなたが実際にストリングを使用して、SRCのURLを取得することができ、独自のメソッドを作成してからちょうど文書の接続を取得するためにその文字列を使用することが分かっ:

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; 
} 

し、その後でメイン:

String embedURL = getSrcString(iframeEl.toString()); 
Document embedDoc = Jsoup.connect(embedURL).get(); 
関連する問題