按类名获取元素用于单击 [英] getelement by class name for clicking

查看:28
本文介绍了按类名获取元素用于单击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 VB.NET 中创建一个带有 Web 浏览器控件的 Windows 窗体,但无法单击下面的代码.

I am creating a windows form in VB.NET with web browser control, but not able to click on the below code.

我可以在按 ID 获取元素时单击,但不能按类名.请帮助我,谷歌搜索对我没有帮助.

I can click when it is get element by ID, but not by classname. Please help me so much of googling didn't help me.

这是我试过的:

For Each Element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("btn btnSearch bold include_WidthButton")
    If Element.OuterHtml.Contains("btn btnSearch bold include_WidthButton") Then
        Element.InvokeMember("click")
    End If
Exit For

推荐答案

没有使用 webbrowser 文档按类获取元素集合的本机函数.但是,您可以使用函数创建自己的集合,并使用 document.all 属性循环遍历所有元素.

There is no native function to get a collection of elements by class using the webbrowser document. However you can create your own collection using a function and looping through all elements using the document.all property.

Function ElementsByClass(document As HtmlDocument, classname As String)
    Dim coll As New Collection
    For Each elem As HtmlElement In document.All
        If elem.GetAttribute("className").ToLower.Split(" ").Contains(classname.ToLower) Then
            coll.Add(elem)
        End If
    Next
    Return coll
End Function

使用方法如下:

Private Sub UpdatBtn_Click(sender As System.Object, e As System.EventArgs) Handles UpdatBtn.Click

    For Each elem As HtmlElement In ElementsByClass(WebBrowser1.Document, "form")
        elem.SetAttribute("className", elem.GetAttribute("className") & " form-control")
    Next

End Sub

我看到您正试图将您的集合基于整个 className 而不是单个类,因此您需要稍作修改.

I see you are trying to base your collection off the entire className instead of an individual class so you would need to slightly modify.

这篇关于按类名获取元素用于单击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆