有更好的方法吗

发布于 2024-11-10 11:07:13 字数 707 浏览 6 评论 0原文

所以我在 vb.net 中有一个循环,它加载网页,填写表单并单击提交

我目前分别使用这些循环,等待网页加载,填写表单,然后单击提交

   Do While Not browser.ReadyState = WebBrowserReadyState.Complete

            System.Windows.Forms.Application.DoEvents()
        Loop

        browser.Document.GetElementById("text").SetAttribute("value", message)

但是

  For Each element As HtmlElement In browser.Document.GetElementsByTagName("input")


        If element.GetAttribute("type") = "submit" Then

            element.InvokeMember("click")

        End If
    Next

我的问题是在循环运行大约第五次之后,它在具有设置属性的行上出错。我有一种感觉,它在尝试填写表单之前没有等待网页加载,这就是它出错的原因。

有谁知道更好的方法来做到这一点?

so i have a loop in vb.net that loads a webpage, fills out a form and clicks submit

I currently am using these for, respectively, waiting for the webpage to load, filling out the form, and clicking submit

   Do While Not browser.ReadyState = WebBrowserReadyState.Complete

            System.Windows.Forms.Application.DoEvents()
        Loop

and

        browser.Document.GetElementById("text").SetAttribute("value", message)

and

  For Each element As HtmlElement In browser.Document.GetElementsByTagName("input")


        If element.GetAttribute("type") = "submit" Then

            element.InvokeMember("click")

        End If
    Next

but my problem is that after around the fifth time the loop is run it srrors on the line with the set attribute. And i have a feeling that it is not waiting for the webpage to load before it tries filling out the form, and that is why it is erroring.

Does anyone know a better way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人生百味 2024-11-17 11:07:13

您可以使用 DocumentCompleted 事件,此事件确保您的文档已准备好,并且所有必需的部分已加载

Private Sub browser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles browser.DocumentCompleted
    ' YOUR FORM FILLING CODE HERE


End Sub

,并且对于表单提交,您可以使用表单的 Submit() 方法,如下所示

browser.Document.GetElementById("text").DOMElement.form.submit()

You can use DocumentCompleted Event, this event ensures your document is ready, and all required sections loaded

Private Sub browser_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles browser.DocumentCompleted
    ' YOUR FORM FILLING CODE HERE


End Sub

and for form submit you can use, forms' submit() method like this

browser.Document.GetElementById("text").DOMElement.form.submit()
和影子一齐双人舞 2024-11-17 11:07:13

也许元素“text”尚未加载或不在页面上?

您可以编写一些 javascript 来完成该任务,并将其注入到页面中。

以下是如何在 JavaScript 中进行“点击”:
https://developer.mozilla.org/en/DOM/document.createEvent

Maybe the element "text" isn't loaded yet or isn't on the page?

You could make some javascript to do that task, and inject it into the page.

Here is how to make a "click" in javascript:
https://developer.mozilla.org/en/DOM/document.createEvent

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文