在VB.NET web浏览器点击一个HTML元素 [英] Clicking a HTML Element in VB.NET WebBrowser

查看:185
本文介绍了在VB.NET web浏览器点击一个HTML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能点击该按钮?

 <一类=findgame数据动作=搜索>去< / A>

我曾尝试

 对于每个元素作为的HtmlElement在WebBrowser1.Document.All
            如果Element.GetAttribute(类名)=findgame然后
                Element.InvokeMember(点击)
                如果我做的msgbox(Element.InnerText)显示开始
            万一
        下一个


解决方案

我不能肯定由code你有显示,但我怀疑你正试图以 InvokeMember 文档之前完全加载。使用 DocumentCompleted 事件设置一个标志或东西,将让你知道它是确定与文档进行交互。这是工作code:

 公共类Form1的
    私人小组Form1_Load的(发送者为对象,E作为EventArgs的)把手MyBase.Load
        WebBrowser1.DocumentText =下; HTML>&下;类别=findgame的href =http://www.google.com>去&下; / A>&下; / HTML>中
    结束小组    私人小组WebBrowser1_DocumentCompleted(发送者为对象,E作为WebBrowserDocumentCompletedEventArgs)处理WebBrowser1.DocumentCompleted
        对于每个元素作为的HtmlElement在WebBrowser1.Document.All
            如果Element.GetAttribute(类名)=findgame然后
                Element.InvokeMember(点击)
            万一
        下一个
    结束小组
末级

How can I click this button?

<a class="findgame" data-action="search">Go</a>

I have tried

For Each Element As HtmlElement In WebBrowser1.Document.All
            If Element.GetAttribute("className") = "findgame" Then
                Element.InvokeMember("click")
                'If I do MsgBox(Element.InnerText) it displays "Go"
            End If
        Next

解决方案

I can't be sure by the code you have shown, but I suspect you are attempting to to InvokeMember before the document is completely loaded. Use the DocumentCompleted event to set a flag or something that will let you know it is ok to interact with the document. This is working code:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        WebBrowser1.DocumentText = "<html><a class=""findgame"" href=""http://www.google.com"">go</a></html>"
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        For Each Element As HtmlElement In WebBrowser1.Document.All
            If Element.GetAttribute("className") = "findgame" Then
                Element.InvokeMember("click")
            End If
        Next
    End Sub
End Class

这篇关于在VB.NET web浏览器点击一个HTML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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