簡體   English   中英

JSoup - 基於文本獲取標簽

[英]JSoup - Get tag based on text

假設我在如下定義的頁面上有 3 個文本框。

<input id="input" type="search" autocomplete="off" role="combobox" placeholder="Search">

<input id="input" type="open" autocomplete="off" role="combobox" placeholder="Open">

<input id="input" type="close" autocomplete="off" role="combobox" placeholder="Close">

我將值 'Open' 作為參數傳遞給 JSoup,JSoup 應該返回如下數據(這是中間文本框的詳細信息)。

<input id="input" type="open" autocomplete="off" role="combobox" placeholder="Open">

JSoup 可以做到這一點嗎?

謝謝你

-Anoop

您需要按屬性選擇標簽:

document.select("input[placeholder=Open]")

UPD:要選擇標簽具有值為“Open”的任何屬性,您需要遍歷所有屬性值:

List<Element> result = document.select("input").stream()
            .filter(input -> hasAttrValue(input, "Open"))
            .collect(Collectors.toList());

hasAttrValue 方法:

private boolean hasAttrValue(Element element, String targetValue) {
    for (Attribute attribute : element.attributes()) {
        if (targetValue.equals(attribute.getValue())) {
            return true;
        }
    }
    return false;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM