使用文档对象在网页上进行操作
我有一个 VB.Net 应用程序,需要从安全的 Intranet 站点打印一堆页面;该应用程序是为非安全网站制作的,但现在该应用程序需要登录才能访问打印所需的网页。
当我使用新的 Sub 时,出现错误“对象引用未设置为对象的实例”,如下所示:
Private Sub SiteLogin()
WebBrowser1.Navigate("http://url/login/")
WebBrowser1.Document.GetElementById("user").SetAttribute("value", "username")
WebBrowser1.Document.GetElementById("password").SetAttribute("value", "mypassword")
WebBrowser1.Document.GetElementById("submit").InvokeMember("click")
End Sub
我从 这个 VB 教程,但我似乎在引用 WebBrowser.Document 对象的方式中遗漏了一些内容。我以前从未使用过 VB.Net,所以我对这个问题不太了解,但是我有以下子程序,它可以在原始程序中工作:
Private Sub WebPrint()
WebBrowser1.Navigate(strUrl)
WebDocLoaded = False
'lblMessage.Text = "Waiting for report page to load"
Do Until WebDocLoaded
System.Windows.Forms.Application.DoEvents()
Loop
End Sub
此子程序在 IE 中打印给定的页面,所以我想我可以使用与登录子对象相同的 WebBrowser1 对象,因为我不知道如何或是否应该为 SiteLogin 子对象声明一个单独的对象。然而,这两个函数都是在定义 WebBrowser1 对象之后调用的。
我只需要能够向设定的 URL 提交一个简单的登录表单,并且这些信息都需要进行硬编码(没有设置位置或登录信息的选项,也不应该有)。如果有其他方法可以通过 VB.Net 登录此站点,或者如果我需要提供更多信息,请告诉我。
I have a VB.Net app that needs to print a bunch of pages from a secure intranet site; the app was made and was working for a non-secure site but now the app needs to login before accessing the web pages necessary to print.
I get the error "Object Reference not set to an instance of an object" when I use my new Sub which follows:
Private Sub SiteLogin()
WebBrowser1.Navigate("http://url/login/")
WebBrowser1.Document.GetElementById("user").SetAttribute("value", "username")
WebBrowser1.Document.GetElementById("password").SetAttribute("value", "mypassword")
WebBrowser1.Document.GetElementById("submit").InvokeMember("click")
End Sub
I got the code from this VB tutorial, but I seem to be missing something in how I'm referencing the WebBrowser.Document object. I've never used VB.Net before so I don't know much about this problem, I have the following sub which DOES work from the original program however:
Private Sub WebPrint()
WebBrowser1.Navigate(strUrl)
WebDocLoaded = False
'lblMessage.Text = "Waiting for report page to load"
Do Until WebDocLoaded
System.Windows.Forms.Application.DoEvents()
Loop
End Sub
This Sub prints the given page in IE, so I figured I could use the same WebBrowser1 object for the login Sub, as I don't know how to or whether I should declare a separate object for the SiteLogin Sub. Both of these functions are called after the WebBrowser1 object is defined however.
I just need to be able to submit a simple login form to a set URL, and this info needs to all be hardcoded (there's no option to set the location or login info nor should there be). If there's another way to log into this site via VB.Net or if I need to give more information let me know.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.Navigate
在页面加载完成之前返回,因此 DOM 尚未准备好 &.Document
不可访问。看; 如何在 VB 中等待 WebBrowser 完全加载.NET?
.Navigate
returns before the page load is complete so the DOM is not ready &.Document
is not accessible.See; How to wait until WebBrowser is completely loaded in VB.NET?