自动网站登录:SetAttribute 不起作用 [英] Auto Website Login: SetAttribute not working

查看:27
本文介绍了自动网站登录:SetAttribute 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个程序仪表板,其中一项功能是用户使用程序中存储的凭据自动登录到网站站点(无需打开 chrome 或 FF).

I'm creating a program dashboard and one feature is that the user is automatically logged into a website site using stored credentials in the program (no need to open chrome or FF).

在程序中,等待任务延迟正在工作,我看到用户名和密码字段在提交之前填充(单击),但是当它尝试提交时,内置在表单中的浏览器表现得好像页面是空的,没有已输入凭据?我应该提一下,我可以看到在表单中输入的用户名和密码,但页面表现得好像没有输入任何内容.我在这里做错了什么?

In the program, await task delay is working, I see the username and password fields populate before submission (click), but when it tries to submit, the browser, built into the form, acts like the page is empty and no credentials have been entered? I should mention that I can see the username and password entered in the form, but the page acts like nothing has been entered. What am I doing wrong here?

旁注:我们要连接的网站上的按钮没有元素 ID,只显示了一个类型...因此 Invokemember("Click") 的解决方法

Side note: The button on the site we're connecting to doesn't have an element ID, only a type is shown...hence the workaround for Invokemember("Click")

感谢任何帮助.

    Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Label3.Visible = False
End Sub
Private Function login_thesite() As Task

    WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
    WebBrowser1.Document.GetElementById("Password").SetAttribute("value", "Password")


    Dim allelements As HtmlElementCollection = WebBrowser1.Document.All
    For Each webpageelement As HtmlElement In allelements
        If webpageelement.GetAttribute("type") = "submit" Then
            webpageelement.InvokeMember("click")
        End If
    Next

End Function

Private Property pageready As Boolean = False

    #Region "Page Loading Functions"
Private Sub WaitForPageLoad()
    AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    While Not pageready
        Application.DoEvents()
    End While
    pageready = False
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        pageready = True
        RemoveHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf PageWaiter)
    End If
End Sub

    #End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If CheckBox1.Checked = True Then
        login_thesite()
        WaitForPageLoad()
    End If

End Sub
Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged

    TextBox1.Text = ""
    TextBox2.Text = ""
    Label3.Visible = True
    WebBrowser1.Navigate("https://thesite.com/#/login")
    WaitForPageLoad()
End Sub
End Class     

推荐答案

这里不需要任何 async 过程.WebBrowser.DocumentCompleted 事件是已经异步调用.DoEvents() 如果不是破坏性的,也同样无用.

You don't need any async procedure here. The WebBrowser.DocumentCompleted event is already invoked asynchronously. DoEvents() is equally useless if not disruptive.

您只需要订阅DocumentCompleted事件并调用Navigate方法让WebBrowser加载远程Html资源即可.

You just need to subscribe to the DocumentCompleted event and call the Navigate method to let the WebBrowser load the remote Html resource.

HtmlDocument 最终加载时,WebBrowser 将发出完成信号,将其状态设置为 WebBrowserReadyState.Complete.

When the HtmlDocument is finally loaded, the WebBrowser will signal its completion setting its state to WebBrowserReadyState.Complete.

关于 Html 输入元素和表单:
在这里,代码假设该 HtmlDocument 中只有一个表单.
可能是这样,但也可能不是.一个 Html Document 可以有多个 Form,每个 Frame 可以有自己的 Document.IFrames 肯定会有一个.

About the Html Input Element and Forms:
here, the code is supposing that there's only one Form in that HtmlDocument.
It may be so, but it may be not. A Html Document can have more than one Form and each Frame can have its own Document. IFrames will have one each for sure.

阅读此答案(C# 代码,但您只需要注释)中的注释,了解有关如何处理多个 Frames/IFrames

Read the notes in this answer (C# code, but you just need the notes) for more information on how to handle multiple Frames/IFrames

Button1 将连接 DocumentCompleted 事件并调用 Navigate().
文档完成后,事件处理程序中的代码将运行并执行登录过程.
然后删除事件处理程序,因为它已完成其任务,您仍然需要将 WebBrowser 用于其他目的.

Button1 will wire up the DocumentCompleted event and call Navigate().
When the Document is completed, the code in the event handler will run and perform the LogIn procedure.
The event handler is then removed, since it has complelted its task and you still need to use the WebBrowser for other purposes.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Button1.Enabled = False
    WebSiteLogIn()
End Sub

Private Sub WebSiteLogIn()
    AddHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
    WebBrowser1.Navigate("https://thesite.com/#/login")    
End Sub

Private Sub PageWaiter(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
    If WebBrowser1.ReadyState = WebBrowserReadyState.Complete Then
        WebBrowser1.Document.GetElementById("username").SetAttribute("value", "Username")
        WebBrowser1.Document.GetElementById("Password").SetAttribute("value", "Password")

        Dim allInputElements = WebBrowser1.Document.Body.All.
            Cast(Of HtmlElement).Where(Function(h) h.TagName.Equals("INPUT")).ToList()

        For Each element As HtmlElement In allInputElements
            If element.GetAttribute("type").ToUpper().Equals("SUBMIT") Then
                element.InvokeMember("click")
            End If
        Next

        RemoveHandler WebBrowser1.DocumentCompleted, AddressOf PageWaiter
        Button1.Enabled = True
    End If
End Sub

这篇关于自动网站登录:SetAttribute 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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