WebBrowser Auto-Populate Open File dialog box
When using WebBrowser to write automatic form filling software, I do not know if you have encountered the state to fill in the document selection form. In this case, it is not possible to directly assign the team HTML elements, you must simulate the method of manual selection (whether there are other solutions, not known), let us consider my solution, as follows:
1, first talk about the normal form of the filling method, in the delphi+webbrowser environment, can use the following code:
1 |
WebBrowser . OleObject . document . all . item( ‘XXX‘ , 0 ).value :=xxx; |
2, for the file selection form, we can first simulate the click, open the File selection dialog box:
1 |
WebBrowser . OleObject . document . all . item( ‘click‘ , 0 ).value; |
3, then we take the enumeration subform method, find the file input location, and the "OK" button to simulate the selection of files:
0102030405060708091011121314151617181920212223242526272829303132333435363738 |
//...
function EnumProc(Hwnd: THandle; LB:
LongInt
):
Boolean
; stdcall;
//...
function EnumProc(Hwnd: THandle; LB:
LongInt
):
Boolean
; stdcall;
var CName:
array
[
0..255
]
of char
;
path:
string
;
i:
integer
;
begin
GetClassName(Hwnd, CName,
255
);
if CName =
‘Edit‘ then
begin
path := extractfilepath(Application
.
ExeName) +
‘检索\‘ + nameStr + ‘
.bmp‘;
for i :=
1 to length(path)
do
begin
PostMessage(Hwnd, WM_CHAR, WPAram(path[i]),
1
);
end
;
//次数模拟选择文件,就是将文件的位置填写到选择框内。
end
;
if (TName =
‘打开(&O)‘
)
or (TName =
‘确定‘
)
then
begin
PostMessage(Hwnd, WM_LBUTTONDOWN,
0
,
0
);
PostMessage(Hwnd, WM_LBUTTONup,
0
,
0
);
//此处模拟点击
end
;
Result :=
True
;
end
;
//...
Procedure FindWin();
//调用
var Hwnd:THandle;
begin
hwnd := FindWindow(
nil
, Title);
//Title为文件选择窗口标题
if Hwnd <>
0 then
begin
EnumChildWindows(Hwnd, @EnumProc, lp);
end
;
end
;
|
3. Matters needing attention:
Upload File Form dialog box
A, select the File dialog window title, under different IE kernel is not the same, I tested on Win7 and XP is the "Select File", "Choose to load the file."
b, in the simulation when filling in, you need to note that when prompted to open the file, the code run is paused, that is, you have to open the simulation in the thread.
C, in the name of the input, do not use the SetWindowText function, this function can not be implemented, can only be sent WM_CHAR message, the way to simulate character input.
4, finally warned everyone, if there is no special encryption algorithm, or other restrictions, try to directly use the HTTP protocol to submit the form, or some things too cumbersome.
WebBrowser Auto-Populate Open File dialog box