簡體   English   中英

替換WebBrowser VB.Net中的文本

[英]Replacing text in WebBrowser VB.Net

如何使用按鈕從WebBrowser中的某些網站中查找和替換某些文本?

<input type="text" value="football"></input>

我想將“足球”更改為“籃球”。

我發現可能可以使用的代碼很少,但我不知道如何使用它們(例如this )。

要執行您要求的操作,可以遍歷所有input標簽並檢查其value屬性。 如果找到匹配項,請將屬性更改為所需的屬性。

HtmlDocument.GetElementsByTagName()將返回一個集合,其中包含在文檔中找到的所有指定類型的標簽。

For Each Tag As HtmlElement In WebBrowser1.Document.GetElementsByTagName("input") 'Iterate through all <input> tags.
    If Tag.GetAttribute("value").ToLower() = "football" Then 'Check if the 'value' attribute is set to 'football'.
        Tag.SetAttribute("value", "basketball") 'Set the attribute to 'basketball' instead.
        Exit For 'Exit the loop.
    End If
Next

如果要將文檔中所有輸入標簽的football替換為basketball ,請刪除“ Exit For行。

還要注意,我使用ToLower()使字符串小寫,從而提供了不區分大小寫的字符串比較。

暫無
暫無

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

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