积极答复者
vb.net自动填写网页表单的问题

问题
答案
-
Imports System.Runtime.InteropServices
'api constant declarations...
Const WM_GETTEXT As Long = &HD
Const WM_GETTEXTLENGTH As Long = &HE
Const GW_ENABLEDPOPUP As Long = 6
Const BM_CLICK As Long = &HF5&
Const GW_CHILD As Long = 5
Const GW_HWNDNEXT As Long = 2'function to retrieve the popup window associated with the form, as well as to find the child windows of the popup...
Private Declare Auto Function GetWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal uCmd As Integer) As IntPtrPublic Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
'sendmessage overload that is used to send messages to the button on the dialog window...
Private Declare Auto Function SendMessage Lib "user32.dll" Alias "SendMessage" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As Integer, ByRef lParam As IntPtr) As IntPtr'sendmessage overloads used to retrieve the window text...
Private Declare Auto Function SendMessageA Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr
<DllImport("User32.dll", CharSet:=CharSet.Auto, Entrypoint:="SendMessage")> _
Public Shared Function SendMessageString(ByVal hwnd As IntPtr, _
ByVal wMsg As Integer, ByVal wparam As Integer, ByVal lparam As System.Text.StringBuilder) As IntPtr
End Function -
Code Snippet
'these next three functions are used to enumerate the Windows handles of all Windows sited on the specified parent...
Function GetChildWindowHandles(ByVal ParentWindowHandle As IntPtr) As ArrayListDim b As Boolean
Dim ptrChild As IntPtr
Dim clsRet As New ArrayList'get first child handle...
ptrChild = GetChildWindowHandle(ParentWindowHandle)Do Until ptrChild.Equals(IntPtr.Zero)
'add to collection of handles...
clsRet.Add(ptrChild)
'get next child...
ptrChild = GetNextWindowHandle(ptrChild)
Loop'return...
Return clsRetEnd Function
Function GetChildWindowHandle(ByVal ParentWindowHandle As IntPtr) As IntPtr
Return GetWindow(ParentWindowHandle, GW_CHILD)
End FunctionFunction GetNextWindowHandle(ByVal CurrentWindowhandle As IntPtr) As IntPtr
Return GetWindow(CurrentWindowhandle, GW_HWNDNEXT)
End Function'this function returns the text of the window, used so that we can confirm that we have the right dialog window...
Function GetWindowText(ByVal WindowHandle As IntPtr) As StringDim ptrRet As IntPtr
Dim ptrLength As IntPtr'get length for buffer...
ptrLength = SendMessageA(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)'create buffer for return value...
Dim sb As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)'get window text...
ptrRet = SendMessageString(WindowHandle, WM_GETTEXT, ptrLength.ToInt32 + 1, sb)'get return value...
Return sb.ToStringEnd Function
全部回复
-
非狐 写: frame已经找到,但你可能不明白我的意思。我是说要根据网页反馈的信息进行下一步操作。比如输入重复号码时,会有一个弹出的对话框(网页中应该是alert)告诉我“号码已经存在”,那么我要在我的程序中得到这个对话框的内容,然后进行下一步的操作。我的问题就是,怎么得到网页中弹出对话框(也就是alert的内容)。我的思路是枚举当前所有窗口,找到这个对话框后用getdlgtext或者GetWindowText得到其中的内容,但不知道具体如何实现,恳请高手指教,非常感谢!
frame对象 就是window对象 第一个成员就是document
直接用document.body.innerhtml就可以得到内容 全部都符合dom
-
非狐 写: 大哥,虽然我很感谢你的热心,但你的回答还是~~
我说了你固然可以得到全部的内容,但是,这个alert只有在特定的条件下出现,比如我输入了重复的,我就根据这个alert知道是重复了,转到重复的处理;加入我输入错误的,那么这个alert又不同,我又要转到另一个错误处理;而如果是正确的,那么就没有alert,我就继续正常的操作,understand?
那么你说的就是alert而不是弹出的网页对话框了
以前说的frames的问题全部都无效 你看不懂是正常 因为我提供的方法和alert无关 误会误会
这个更好办 不过需要分析原来的代码作检查产生alert的脚本究竟在哪里 你找到检查操作的事件就可以知道代码块
document.scripts
然后找到这个script块的index(从头开始 一个一个看innertext) 找到那个块
用你自己的script代码代替他的相应脚本
比如把它的alert("重复") 修改成 document.body.title="重复"
这个块被你替换了以后 只要出现重复 网页的标题就变成“重复” 你直接访问title就知道结果了
当然在下一次提交前要把标题重新改成别的
-
Imports System.Runtime.InteropServices
'api constant declarations...
Const WM_GETTEXT As Long = &HD
Const WM_GETTEXTLENGTH As Long = &HE
Const GW_ENABLEDPOPUP As Long = 6
Const BM_CLICK As Long = &HF5&
Const GW_CHILD As Long = 5
Const GW_HWNDNEXT As Long = 2'function to retrieve the popup window associated with the form, as well as to find the child windows of the popup...
Private Declare Auto Function GetWindow Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal uCmd As Integer) As IntPtrPublic Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
'sendmessage overload that is used to send messages to the button on the dialog window...
Private Declare Auto Function SendMessage Lib "user32.dll" Alias "SendMessage" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As Integer, ByRef lParam As IntPtr) As IntPtr'sendmessage overloads used to retrieve the window text...
Private Declare Auto Function SendMessageA Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal Msg As Integer, _
ByVal wParam As IntPtr, ByRef lParam As IntPtr) As IntPtr
<DllImport("User32.dll", CharSet:=CharSet.Auto, Entrypoint:="SendMessage")> _
Public Shared Function SendMessageString(ByVal hwnd As IntPtr, _
ByVal wMsg As Integer, ByVal wparam As Integer, ByVal lparam As System.Text.StringBuilder) As IntPtr
End Function -
Code Snippet
'these next three functions are used to enumerate the Windows handles of all Windows sited on the specified parent...
Function GetChildWindowHandles(ByVal ParentWindowHandle As IntPtr) As ArrayListDim b As Boolean
Dim ptrChild As IntPtr
Dim clsRet As New ArrayList'get first child handle...
ptrChild = GetChildWindowHandle(ParentWindowHandle)Do Until ptrChild.Equals(IntPtr.Zero)
'add to collection of handles...
clsRet.Add(ptrChild)
'get next child...
ptrChild = GetNextWindowHandle(ptrChild)
Loop'return...
Return clsRetEnd Function
Function GetChildWindowHandle(ByVal ParentWindowHandle As IntPtr) As IntPtr
Return GetWindow(ParentWindowHandle, GW_CHILD)
End FunctionFunction GetNextWindowHandle(ByVal CurrentWindowhandle As IntPtr) As IntPtr
Return GetWindow(CurrentWindowhandle, GW_HWNDNEXT)
End Function'this function returns the text of the window, used so that we can confirm that we have the right dialog window...
Function GetWindowText(ByVal WindowHandle As IntPtr) As StringDim ptrRet As IntPtr
Dim ptrLength As IntPtr'get length for buffer...
ptrLength = SendMessageA(WindowHandle, WM_GETTEXTLENGTH, IntPtr.Zero, IntPtr.Zero)'create buffer for return value...
Dim sb As New System.Text.StringBuilder(ptrLength.ToInt32 + 1)'get window text...
ptrRet = SendMessageString(WindowHandle, WM_GETTEXT, ptrLength.ToInt32 + 1, sb)'get return value...
Return sb.ToStringEnd Function
-
Code Snippet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim ptrDialogWindow As IntPtr = FindWindow(Nothing, "Windows Internet Explorer")
Dim clsChildHandles As ArrayList = GetChildWindowHandles(ptrDialogWindow)
For Each ptrHandle As IntPtr In clsChildHandles
Dim str As String = GetWindowText(ptrHandle)
If str <> "" And str <> "OK" Then
MsgBox(str)
End IfNext
End Sub