C++builer6.0用CppWebBrowser模拟点击html网页的问题

BCB 2006-11-02 10:03:12
看了老贴子中蒋老大的CppWebBrowser模拟点击html网页的的程序片断,
我已成功的试验好。可模拟点击:表单中上传文件的标签就是不成功,
如果人工键入文件名串,我的程序可以显示出该串,说明我的程序没有
问题
d:\chaFS\html\upload.html的内容如下:

html>
<FORM id=Form1 name=Form1 action=uploadfile.aspx
method=post encType=multipart/form-data>
<br>文件标题
<INPUT name=titleBox>
<br>上传文件
<INPUT type=file name=File1>
</FORM>
</html>
可以看到表单中有两个标签,一个是普通的文本标签titleBox
另一个是上传文件名标签File1,点击“浏览”可以选文件名,也用在网页直接
键入文件名
现在我用CppWebBrowser模拟点击File1,就是不成功
...全文
550 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
蒋晟 2006-11-03
  • 打赏
  • 举报
回复
By design, only the browser user should be able to enter text into the box that indicates the name of the file to upload. However, SendKeys provides a way for a webbrowser application to bypass this restriction and put text of the programmer's choice into the field that specifies the file name.

HTMLFormElementClass form = (HTMLFormElementClass)forms["form1"].DomElement;
HTMLInputElementClass fileupload = (HTMLInputElementClass)form.item("data", 0);
fileupload.setAttribute("autocomplete", "false", 0);
IntPtr CurrentForegroundWindow = Win32.GetForegroundWindow();
if (CurrentForegroundWindow != this.Handle)
Win32.SetForegroundWindow(this.Handle);
webBrowser1.Focus();
fileupload.focus();
Debug.Assert(UploadFileQue.Peek().Length > 0);
SendKeys.SendWait(UploadFileQue.Peek());
if (CurrentForegroundWindow != this.Handle)
Win32.SetForegroundWindow(CurrentForegroundWindow);

reference
http://msdn.microsoft.com/msdnmag/issues/05/01/CQA/
http://support.microsoft.com/kb/266087
BCB 2006-11-03
  • 打赏
  • 举报
回复
内疚,改行搞仪表了,整天接触的现场仪表、DCS、PLC,还好不亦乐乎
huzhangyou 2006-11-03
  • 打赏
  • 举报
回复
很久没有看到BCB的出现了
久仰!
BCB 2006-11-03
  • 打赏
  • 举报
回复
蒋老大出山,有希望。
原来这种设置文件名的限制是微软考虑安全的因素。
改用虚拟击键的方法实现,OK,再试试
BCB 2006-11-02
  • 打赏
  • 举报
回复
IHTMLInputFileElement *pText;
它的几个子程序(如put_value()等)都试过了,put_status(不知用处?)
我已黔驴技穷
BCB 2006-11-02
  • 打赏
  • 举报
回复
下列方法设置标签txt的串值val(上传文件名),就是不成功!不知为何??????????????
bool IE::SetUploadFile(String txt,String val)
{
bool r=false;
IHTMLElementCollection *pCs=NULL;
if (pHTMLDoc!=NULL)
if (pHTMLDoc->get_all(&pCs)==S_OK)
if (pCs!=NULL)
{
VARIANT ns,zero;
ns.vt=VT_BSTR;
ns.bstrVal=WideString(txt).c_bstr();
zero.vt=VT_UI4;
zero.lVal=0;
LPDISPATCH iDisp=NULL;
if (pCs->item(ns,zero,&iDisp)==S_OK)
if (iDisp!=NULL)
{
IHTMLInputFileElement *pText=NULL;
if (iDisp->QueryInterface(
IID_IHTMLInputFileElement,(void**)&pText)==0)
if (pText!=NULL)
{
pText->put_value(WideString(val).c_bstr());
// 总是无效!!!!!!!!!!!!!!!!!!!!!!!!!!!
pText->Release();
r=true;
}
iDisp->Release();
}
pCs->Release();
}
return(r);
}
BCB 2006-11-02
  • 打赏
  • 举报
回复
下面我做了简单的封装,方法GetUploadFile(标签名)取标签txt中的内容,成功!没有问题
String IE::GetUploadFile(String txt)
{
String s="";
IHTMLElementCollection *pCs=NULL;
if (pHTMLDoc!=NULL)
if (pHTMLDoc->get_all(&pCs)==S_OK)
if (pCs!=NULL)
{
VARIANT ns,zero;
ns.vt=VT_BSTR;
ns.bstrVal=WideString(txt).c_bstr();
zero.vt=VT_UI4;
zero.lVal=0;
LPDISPATCH iDisp=NULL;
if (pCs->item(ns,zero,&iDisp)==S_OK)
if (iDisp!=NULL)
{
IHTMLInputFileElement *pText=NULL;
if (SUCCEEDED(iDisp->QueryInterface(
IID_IHTMLInputFileElement,(void**)&pText)))
if (pText!=NULL)
{
wchar_t *ws=new wchar_t[128];
pText->get_value(&ws);
s=WideString(ws);
delete ws;
pText->Release();
}
iDisp->Release();
}
pCs->Release();
}
return(s);
}

13,870

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧