2017-07-19 5 views
0

を取得するために失敗します。はHtmlUnitとXPath私は、このウェブからの入力要素を取得しようとしていますHTML要素

https://www.milanuncios.com/textos-del-anuncio/?demandax=n&c=131&idlocalidad=8&p=almeria&x=27&y=0

入力自体は次のとおりです。

<input class="inputs campoGrande" name="marca" value="" maxlength="120" size="12" id="marca" tabindex="1" type="text"> 

私は別のアプローチを試してみましたが、私がいつも得意とするのは「ヌル」リファレンスです:

HtmlInput inputZona = (HtmlInput) currentPage.getElementById("marca"); 
HtmlInput inputZona = (HtmlInput) currentPage.getFirstByXPath("//input[@name='marca']"); 

同じ結果のJSoupも試しました:-(

HTMLUnitとXPAthの両方で項目を取得できません。

これは何が起こっているのですか?事前に

おかげで、

ホセ

+1

このページはXHTMLなので、既定の名前空間またはワイルドカードXPathとして宣言することもできます。 // *:入力 – chrisis

+0

@chrisis、ありがとう。私はXPath(/ html/body/div/form ...)で絶対パスを使用することでそれを行うことができました。 HTMLUnitで正しく処理されなかった唯一の入力項目である理由を実際に理解していない – kankamuso

答えて

1

このコードは、HtmlUnitの最新バージョン(2.28-SNAPSHOT)とここに動作します。

public static void main(String[] args) throws Exception { 
     WebClient webClient = new WebClient(BrowserVersion.BEST_SUPPORTED); 
     HtmlPage page = webClient.getPage("https://www.milanuncios.com/textos-del-anuncio/?demandax=n&c=131&idlocalidad=8&p=almeria&x=27&y=0"); 
     System.out.println(page.asXml()); 

     HtmlInput inputZona = (HtmlInput) page.getElementById("marca"); 
     System.out.println(inputZona.asXml()); 
    } 
関連する問題