有更好的方法吗
所以我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用 DocumentCompleted 事件,此事件确保您的文档已准备好,并且所有必需的部分已加载
,并且对于表单提交,您可以使用表单的 Submit() 方法,如下所示
You can use DocumentCompleted Event, this event ensures your document is ready, and all required sections loaded
and for form submit you can use, forms' submit() method like this
也许元素“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