0

嗨,我正在尝试使用 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

结束类

4

2 回答 2

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
于 2013-05-17T15:16:55.510 回答
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
于 2013-05-17T17:08:10.773 回答