VB点击javascript button / img

时间:2013-05-17 14:20:20

标签: javascript vb.net onclick

您好我正在尝试使用VB.NET点击网站上的按钮

我在.AHK中使用的命令是

Pwb.document.getElementsByTagName("IMG")[7].click

按钮/ Img HTML在下面。

<img width="34" height="34" style="cursor: pointer;" onclick="doSubmit()" alt="" src="../../images/SC5Login07.jpg" border="0" complete="complete"/>

我注意到它在点击时运行了一些javascript。那是在

之下
function doSubmit() {
            //Submit the form
            if (document.getElementById("txtDBName").value == "") {
            //Login security modifications - End
                alert('You must select a Database');
            }
            else {
                if (document.getElementById("txtUserName").value == "") {
                    alert('Please enter your Tesseract user id and password');
                }
                else {
                //Set txtformEvent
                    document.getElementById("txtFormEvent").value = "onSubmit";
                    //Submit the form
                    document.getElementById("frmLoginLaunch").submit();
                }
            }                   
        }

现在我知道有一个GetElementById,但由于缺少ID,我无法工作。我希望它会像在AHK中那样容易,但它看起来并不那样。

基本上有没有办法调用这个dosubmit()函数或模拟点击图像?

希望有人可以提供帮助 感谢

谢谢你这是我的登录代码

Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    WebBrowser1.Navigate("http://caan/SC5/SC_Login/aspx/login_launch.aspx?SOURCE=ESOLBRANCHLIVE")

    While WebBrowser1.ReadyState <> WebBrowserReadyState.Complete
        Application.DoEvents()
    End While

    WebBrowser1.Document.GetElementById("txtUserName").SetAttribute("value", "kieranw")
    WebBrowser1.Document.GetElementById("txtPassword").SetAttribute("value", "kieranw")

    Dim allImgTags As HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("img")
    If allImgTags IsNot Nothing Then
        For Each img As HtmlElement In allImgTags
            If img.GetAttribute("src").Contains("/images/SC5Login07.jpg") Then
                img.InvokeMember("Click")
                Exit For
            End If
        Next img
    End If

End Sub

结束班

2 个答案:

答案 0 :(得分:0)

您没有说您的VB程序是Web应用程序,控制台应用程序等,但如果您可以使用jQuery,那么您可以这样做:

$("img[src$='SC5Login07.jpg']").trigger('click');

或:

$("img[src$='SC5Login07.jpg']").click();
编辑:看起来你正在使用Winforms(这不是这类东西的最佳选择,IMO)。在这种情况下,尝试这样的事情:

Dim browser As New WebBrowser
browser.Navigate("your_intranet_url")

While browser.ReadyState <> WebBrowserReadyState.Complete
  Application.DoEvents()
End While

Dim allImgTags As HtmlElementCollection = browser.Document.GetElementsByTagName("img")
If allImgTags IsNot Nothing Then
  For Each img As HtmlElement In allImgTags
    If img.GetAttribute("src").Contains("/images/SC5Login07.jpg") Then
      img.InvokeMember("Click")
      Exit For
    End If
  Next img
End If
编辑#2:ron tornamb的代码更接近原始的AHK代码,所以如果你想通过索引找到img,请使用:

Dim browser As New WebBrowser
browser.Navigate("your_intranet_url")

While browser.ReadyState <> WebBrowserReadyState.Complete
  Application.DoEvents()
End While

Dim img As HtmlElement = browser.Document.GetElementsByTagName("img")(7)
If im IsNot Nothing Then
    img.InvokeMember("Click")
    End If
End If

答案 1 :(得分:0)

要在VB.Net中执行单击,首先需要等待文档加载,然后在元素上发出InvokeMember(“click”)命令,如下所示:

Private Sub Pwb_DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles Pwb.DocumentCompleted
    Dim elem As HtmlElement = Pwb.Document.GetElementsByTagName("IMG")(7)
    elem.InvokeMember("click")
End Sub