简体   繁体   English

如何在.NET Visual Basic中按名称获取元素

[英]How to get element by name in .net visual basic

I know how to get an element by its ID in visual basic (.net) with getelementbyid. 我知道如何使用getelementbyid在Visual Basic(.net)中通过其ID获取元素。

But how do I get an element by its name attribute. 但是如何通过其name属性获取元素。

eg <input id="123" name="**123**"> 例如<input id="123" name="**123**">

And if so, how do I interact with said element. 如果是这样,我如何与所述元素进行交互。

Thanks 谢谢

Again the assumtion lies with you using the webbroswer 同样,使用webbroswer承担责任

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

    'NAVIGATE
    WebBrowser1.Navigate("whereveryourgoing")
    'Waiting for page to load function
    WaitForPageLoad()

    'Get Element by Name
    Dim fb_button As String = String.Empty
    For Each element As HtmlElement In WebBrowser1.Document.All
        If InStr(element.Name, "loginBox") Then
            fb_button = element.Id
            'MsgBox(fb_button & " Found! ")
            'MsgBox(element.InnerHtml.Length.ToString)
            'MsgBox(element.InnerHtml.ToString)
        End If
    Next

    '\/ - Perform Actions
    WebBrowser1.Document.GetElementById(fb_button).SetAttribute("value", Password) 'or InvokeMember("submit") or InvokeMember("click")
    MsgBox("Done")

End Sub

Source 资源

If I understand you correctly, you will need to do something like this: 如果我对您的理解正确,则需要执行以下操作:

            Dim myInput As HtmlInputControl = CType(e.Item.FindControl("123"), HtmlInputControl)

Then do stuff with the input you just created 然后使用刚刚创建的输入进行处理

            myInput.Enabled = False

OR 要么

You could just add runat="server" to your element <input id="123" name="**123**" runat="server"> and then just reference the element by its id server side as long as the function you're referencing your element in isn't a shared function (if it's shared, you'll need a new instance of the class it was created in to reference it). 您可以将runat="server"添加到元素<input id="123" name="**123**" runat="server"> ,然后只要其ID服务器端引用该元素即可,只要该函数您引用的元素不是共享函数(如果是共享函数,则需要创建该类的新实例来引用它)。

           123.Enabled = False

Also, I'm not sure if you're just doing it to show the example, but you might run into some surprises if you use asterisks in your element names due to it's use as a wildcard character. 另外,我不确定是否只是为了显示示例而已,但是如果在元素名称中使用星号,则由于将其用作通配符,您可能会感到有些意外。 Generally, you want to keep your attributes with numbers and letters (plus hyphens and underscores) only ;) 通常,您只想保留数字和字母(加上连字符和下划线)的属性;)

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM