我在MFC中用Microsoft Web浏览器加载了网易的网页。我想要程序找到这个登录按钮元素,<input type="submit" value="登录" />,并点击它。pobjAllElement->item(COleVariant(strName),COleVariant((long)0),&pDisp);
这句代码只能通过ID 或 NAME 来找到元素吧。但是我现在想找到一个按钮。只能通过按钮的type和value来找,应该怎样找到这个按钮啊?
这句代码只能通过ID 或 NAME 来找到元素吧。但是我现在想找到一个按钮。只能通过按钮的type和value来找,应该怎样找到这个按钮啊?
IHTMLElement * pElem = NULL;
//LPDISPATCH pDispatch;
IDispatch * pDispatch=NULL;
IHTMLDocument2 * pDoc=NULL;
IHTMLElementCollection * pAllElem = NULL;
VARIANT name;
BSTR tag; //for Element's tagName
// long p; //for IHTMLElementCollection's item number
pDispatch=m_ie.GetDocument();
ASSERT(pDispatch);
HRESULT hr=pDispatch->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);
pDoc->get_all(&pAllElem);
ASSERT(pAllElem);
if(pAllElem!=NULL)
{
//AfxMessageBox("gethere1");
for(int i=0;i<100;i++)//遍历所有元素
{
name.lVal = i;
pAllElem->item(name,name,(IDispatch**)&pElem);//获取元素对象指针
ASSERT(pElem);///////////////////////运行出错!!!!!!!!!!!!!
pElem->get_tagName(&tag);//获取tagName
CString ss(tag); //转化成CString
if(ss.CompareNoCase("Input") ==0)//比较是否为table
AfxMessageBox(ss); //在对话框中显示tagName
::SysFreeString(tag);
}
}
2、pAllElem->item(name,name,(IDispatch**)&pElem);//获取元素对象指针
不知是我眼花还是你忘了贴,我没看到你的name这个变量什么时候赋的值,可能这就是你的那句运行出错的原因。你可以从0到itemCount遍历,获取每个元素的指针。多看看MSDN,IHTMLElementCollection说的还是比较清楚的。
可能是因为你只赋值了,没有指定类型。
循环前 执行一下:
name.vt = VT_I4;
试试
模拟点击登录按钮是不是只要
SendMessage(lhwnd, WM_LBUTTONDOWN, 0,0)
SendMessage(lhwnd, WM_LBUTTONUP, 0,0) 就可以了?但是怎么得到登录按钮的x,y,好点击那里呢?
我想得到按钮上的“登陆”文字,请问这样写对吗?为什么总是报 “应用程序错误”?
IHTMLElement * pElem = NULL;
//LPDISPATCH pDispatch;
IDispatch * pDispatch=NULL;
IHTMLDocument2 * pDoc=NULL;
IHTMLElementCollection * pAllElem = NULL;
VARIANT name;
BSTR tag; //for Element's tagName
// long p; //for IHTMLElementCollection's item number
pDispatch=m_ie.GetDocument();
//ASSERT(pDispatch);
HRESULT hr=pDispatch->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);
pDoc->get_all(&pAllElem);
// ASSERT(pAllElem);
if(pAllElem!=NULL)
{
name.vt = VT_I4;//AfxMessageBox("gethere1");
for(int i=0;i<100;i++)//遍历所有元素
{
name.lVal = i;
pAllElem->item(name,name,(IDispatch**)&pElem);//获取元素对象指针
ASSERT(pElem);///////////////////////运行出错!!!!!!!!!!!!!
pElem->get_outerText(&tag);//获取tagName
CString ss(tag); //转化成CString
if(ss.CompareNoCase("登录") ==0)//比较是否为table
AfxMessageBox(ss); //在对话框中显示tagName
::SysFreeString(tag);
}