求教高手,怎样在IE控件中载入的网页中找到一个超链接。

xingxiangshicheng 2006-07-15 08:44:51
我在MFC中用 Microsoft Web浏览器 载入了一个网页。我想让程序自动点击页面里的一个超链接。

BSTR tag;
spElement->get_outerText(&tag);
CString ss(tag);
if(ss=="首页")
spElement->click();

我发现用上面的代码可以找到这种类型的超链接:<a href=http://www.sina.com.cn/>首页</a>
但是找不到这种类型的超链接:
<a href="http://www.sina.com.cn" style="color:yellow;text-decoration:none;font-size:12px;font-weight:bold" target="_top">首页</a>
我想找到这种类型的超链接,请问要用什么方法?
...全文
516 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingxiangshicheng 2006-07-15
  • 打赏
  • 举报
回复
哦,是的。我刚才又试过可以了。可能是上次网速太慢影响了的原因。不过这个网页确实不行
http://asiafind.com/p/register.cgi? who=UmFuZG9tSVZk7aOWWmArZXBN6S1Wl9IAtTnlY8PcDx3jxzubJYZo70oeZlS/sMQ_13t0kb5GHQFkrMmx7Rw1QUS54fji_liK/jJ47Ec2wZFW0Of5ExrjABLR69HhMTMNbI4Ol2m0ZFjdhd6x6mDhKR/nKeB4MJ7HzfLxVzywInSUtFFUVIWNF5bPbfNHafuPD/yYdq/sqceBWUfa5ZrM7g--&site=ffz&dcb=asiafind
这个网页最下面有一个 点选此键继续 按钮。把下面的 if (bstrValue==_T("登录"))改成
if (bstrValue==_T("点选此键继续 ")) 确实不起作用
lion_wing 2006-07-15
  • 打赏
  • 举报
回复
我这儿改成“百度搜索”可以呀,只不过,我没输入搜索的关键词,它不停的刷新。
xingxiangshicheng 2006-07-15
  • 打赏
  • 举报
回复
又如我加载 http://www.baidu.com
把下面的 if (bstrValue==_T("登录"))改成 if (bstrValue==_T("百度搜索"))
运行以后也是一点反映也没有。我实在找不出问题出在哪里
xingxiangshicheng 2006-07-15
  • 打赏
  • 举报
回复
谢谢了,lion_wing大哥。还有一个问题。下面是你上次给我的代码。但是奇怪得很。只有在网易首页才有用,我换个网页加载。比如加载 http://www.sina.com.cn 就不管用了。这个页面上也有一个一模一样的登录按钮。又如我看了半天也不知道是什么原因。请问这是为什么呢?

void CTest4Dlg::OnDocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT FAR* URL)
{
CComQIPtr<IWebBrowser2, &IID_IWebBrowser2> pWeb;
CComQIPtr<IHTMLDocument2,&IID_IHTMLDocument2> pDoc2;
pWeb = pDisp;
pWeb->get_Document((IDispatch**)&pDoc2);

if (m_ie.GetDocument() != pDoc2) return;
CComQIPtr<IHTMLElementCollection> pHTMLColl;
HRESULT hr = pDoc2->get_all( &pHTMLColl);
if(hr == S_OK)
{
long lngLen;
hr=pHTMLColl->get_length(&lngLen);
if (hr != S_OK) return;
for (int i=0;i<lngLen;i++)
{
CComQIPtr<IDispatch> pDisp;
CComVariant varTemp(i);
hr = pHTMLColl->item( varTemp, varTemp, &pDisp);
if (hr == S_OK)
{

CComQIPtr<IHTMLElement> pElem;
hr =pDisp->QueryInterface(IID_IHTMLElement,(void**)&pElem);
if (hr == S_OK)
{
CComVariant varAttribute;
CComBSTR bstrName("VALUE");
CComBSTR bstrValue;
hr=pElem->getAttribute(bstrName,0,&varAttribute);
if(hr==S_OK)
{
if (SUCCEEDED(varAttribute.ChangeType(VT_BSTR)))
{

bstrValue = varAttribute.bstrVal;
if (bstrValue==_T("登录"))
{
pElem->click();

return;
}
}
}

}
}
}
}

// TODO: Add your control notification handler code here

}
xingxiangshicheng 2006-07-15
  • 打赏
  • 举报
回复
谢谢了,lion_wing大哥。还有一个问题。下面是你上次给我的代码。但是奇怪得很。只有在网易首页才有用,我换个网页加载。比如加载 http://www.sina.com.cn 就不管用了。这个页面上也有一个一模一样的登录按钮。又如我看了半天也不知道是什么原因。请问这是为什么呢?

void CTest4Dlg::OnDocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT FAR* URL)
{
CComQIPtr<IWebBrowser2, &IID_IWebBrowser2> pWeb;
CComQIPtr<IHTMLDocument2,&IID_IHTMLDocument2> pDoc2;
pWeb = pDisp;
pWeb->get_Document((IDispatch**)&pDoc2);

if (m_ie.GetDocument() != pDoc2) return;
CComQIPtr<IHTMLElementCollection> pHTMLColl;
HRESULT hr = pDoc2->get_all( &pHTMLColl);
if(hr == S_OK)
{
long lngLen;
hr=pHTMLColl->get_length(&lngLen);
if (hr != S_OK) return;
for (int i=0;i<lngLen;i++)
{
CComQIPtr<IDispatch> pDisp;
CComVariant varTemp(i);
hr = pHTMLColl->item( varTemp, varTemp, &pDisp);
if (hr == S_OK)
{

CComQIPtr<IHTMLElement> pElem;
hr =pDisp->QueryInterface(IID_IHTMLElement,(void**)&pElem);
if (hr == S_OK)
{
CComVariant varAttribute;
CComBSTR bstrName("VALUE");
CComBSTR bstrValue;
hr=pElem->getAttribute(bstrName,0,&varAttribute);
if(hr==S_OK)
{
if (SUCCEEDED(varAttribute.ChangeType(VT_BSTR)))
{

bstrValue = varAttribute.bstrVal;
if (bstrValue==_T("登录"))
{
pElem->click();

return;
}
}
}

}
}
}
}

// TODO: Add your control notification handler code here

}
lion_wing 2006-07-15
  • 打赏
  • 举报
回复
看了你的代码,可能在页面中有多个框架时,会多次触发,可能导致页面中后面的“首页”链接得不到:
你可以在void CTest2Dlg::OnDownloadCompleteExplorer1(LPDISPATCH pDisp, VARIANT FAR* URL) 加入下面代码:

CComQIPtr<IWebBrowser2, &IID_IWebBrowser2> pWeb;
CComQIPtr<IHTMLDocument2,&IID_IHTMLDocument2> pDoc2;
pWeb = pDisp;
pWeb->get_Document((IDispatch**)&pDoc2);

if (m_ie.GetDocument() != pDoc2) return;
xingxiangshicheng 2006-07-15
  • 打赏
  • 举报
回复
弄错了,全部代码如下:
void CTest2Dlg::OnDownloadCompleteExplorer1()
{


IHTMLElementCollection *objAllElement=NULL;
IHTMLDocument2 *objDocument=NULL;

objDocument=(IHTMLDocument2 *)m_ie.GetDocument(); //由控件得到IHTMLDocument2接口指针
objDocument->get_all(&objAllElement); //得到网页所有元素的集合

IHTMLElement * pElem = NULL;
VARIANT name;
CComBSTR tag;
long a;
objAllElement->get_length(&a);

name.vt=VT_I4;;
for(int i=0;i<a;i++)//遍历所有元素
{
name.lVal = i;

IDispatch * pDispatch=NULL;
objAllElement->item(name,name,&pDispatch);
IHTMLElement* spElement;
pDispatch->QueryInterface(IID_IHTMLElement, (void**)&spElement);

BSTR tag;
spElement->get_outerText(&tag);
CString ss(tag);
if(ss=="首页")

spElement->click();


// TODO: Add your control notification handler code here
}

}
xingxiangshicheng 2006-07-15
  • 打赏
  • 举报
回复
整段代码如下

void CTest2Dlg::OnDownloadCompleteExplorer1()
{

IHTMLElementCollection *objAllElement=NULL;
IHTMLDocument2 *objDocument=NULL;

objDocument=(IHTMLDocument2 *)m_ie.GetDocument(); //由控件得到IHTMLDocument2接口指针
objDocument->get_all(&objAllElement); //得到网页所有元素的集合



IHTMLElement * pElem = NULL;
VARIANT name;
CComBSTR tag;
long a;
objAllElement->get_length(&a);

name.vt=VT_I4;;
for(int i=0;i<a;i++)//遍历所有元素
{
name.lVal = i;

IDispatch * pDispatch=NULL;
objAllElement->item(name,name,&pDispatch);
IHTMLElement* spElement;
pDispatch->QueryInterface(IID_IHTMLElement, (void**)&spElement);

BSTR tag;
spElement->get_outerText(&tag);
CString ss(tag);
if(ss=="首页")


spElement->click();









// TODO: Add your control notification handler code here
}

}





// TODO: Add your control notification handler code here
IHTMLElementCollection *objAllElement=NULL;
IHTMLDocument2 *objDocument=NULL;
CString strUrl,strTemp;
strUrl=m_ie.GetLocationURL();//得到当前网页的URL
if(strUrl.IsEmpty())
return;
objDocument=(IHTMLDocument2 *)m_ie.GetDocument(); //由控件得到IHTMLDocument2接口指针
objDocument->get_all(&objAllElement); //得到网页所有元素的集合

//由于所有页面下载完后都会执行这个函数,所以必须根据URL判断消息来源网页

if(strUrl=="http://asiafind.com/go/g795175")
{


/* CComPtr<IDispatch>pDisp;
CComPtr<IDispatch>pDisp1;

if(HasItem(objAllElement,"username")==true) //voteform为投票选项所在的Form
{
// MessageBox("有了");
objAllElement->item(COleVariant("username"),COleVariant((long)0),&pDisp);

if(pDisp==NULL)
MessageBox("没有找到");
IHTMLInputTextElement * pInput = NULL;
IHTMLInputTextElement * pInput1 = NULL;



pDisp.QueryInterface(&pInput);
pInput->put_value(L"hhrhhrhhr");
objAllElement->item(COleVariant("password"),COleVariant((long)0),&pDisp1);

pDisp1.QueryInterface(&pInput1);

pInput1->put_value(L"hanghaorang1314");
BSTR bstrhtml;
pInput1->get_type(&bstrhtml);
CString str(bstrhtml);
// AfxMessageBox(str); */
////////////////////////////////////////////////////////////////
IHTMLElement * pElem = NULL;
VARIANT name;
CComBSTR tag;
long a;
objAllElement->get_length(&a);

name.vt=VT_I4;;
for(int i=0;i<a;i++)//遍历所有元素
{
name.lVal = i;

IDispatch * pDispatch=NULL;
objAllElement->item(name,name,&pDispatch);
IHTMLElement* spElement;
pDispatch->QueryInterface(IID_IHTMLElement, (void**)&spElement);

BSTR tag;
spElement->get_outerText(&tag);
CString ss(tag);
//if(ss=="外经贸大法中国际课程班")
//AfxMessageBox(ss);
//spElement->click();
//}








// TODO: Add your control notification handler code here
}
}
}
类库 2006-07-15
  • 打赏
  • 举报
回复
你都没说你怎么得到spElement的,谁知道是哪里出错了呢

3,056

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC HTML/XML
社区管理员
  • HTML/XML社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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