wjdashwood
asked on
Retrieve ‘name’ from ‘textarea’ or ‘input’ html tag using IHTMLElement interface
Does anyone know if it’s possible to retrieve the ‘name’ attribute from a ‘textarea’ or ‘input’ html tag using the IHTMLElement interface or do I have to use IHTMLTextAreaElement / IHTMLInputElement interfaces? Using IHTMLTextAreaElement isn’t such an issue but as the IHTMLInputElement interface requires IE 5.0 I was hoping I might be able to avoid it as some of my users may still be on IE 4.0.
Any kind of work around would be welcome. To give some background, my program searches sequentially through a page and grabs the inner text and the name of any ‘textarea’ or ‘input’ elements on the page.
Any help would be much appreciated.
Thanks!
Any kind of work around would be welcome. To give some background, my program searches sequentially through a page and grabs the inner text and the name of any ‘textarea’ or ‘input’ elements on the page.
Any help would be much appreciated.
Thanks!
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks for the comments. Firstly, I've tried using get_innerHTML although the entire body doesn't seem to fit in the BSTR. It's not an ideal solution but not a bad idea :)
I also tried implementing it inside a loop and evaluated each element to find an input or textarea tag. Is there a way of retireving the whole tag and contents as a string from the IHTMLElement? get_innerHTML only gets the text from inside the textarea tag. If it's any help, my code is shown below.
IHTMLDocument2 *pHtmlDoc = NULL;
if (SUCCEEDED(GetDHtmlDocumen t(&pHtmlDo c)) && (pHtmlDoc != NULL))
{
IHTMLElementCollection* pColl = NULL;
if (SUCCEEDED(pHtmlDoc->get_a ll(&pColl) ) && (pColl != NULL))
{
long nLength = 0;
pColl->get_length(&nLength );
for (long nCount = 0; nCount < nLength; nCount++)
{
COleVariant vIdx(nCount, VT_I4);
IDispatch* pElemDispatch = NULL;
IHTMLElement * pElem = NULL;
if (SUCCEEDED(pColl->item(vId x, vIdx, &pElemDispatch))
&& (pElemDispatch != NULL))
{
if (SUCCEEDED(pElemDispatch-> QueryInter face(IID_I HTMLElemen t,
(void**)&pElem)) && (pElem != NULL))
{
BSTR bstrTagName;
CString sTempTagName;
if (!FAILED(pElem->get_tagNam e(&bstrTag Name)))
{
sTempTagName = bstrTagName;
sTempTagName.MakeLower();
SysFreeString(bstrTagName) ;
}
if ((sTempTagName == _T("input")) || (sTempTagName == _T("textarea")))
{
BSTR bstrText;
pElem->get_innerHTML(&bstr Text);
SysFreeString(bstrText);
}
pElem->Release();
}
pElemDispatch->Release();
}
}
pColl->Release();
}
pHtmlDoc->Release();
}
Secondly, Dan, I'm writing the HTML for the program so all <input> or <textarea> tags must have a name. I only use ID's if I need to detect a click on that item. As shown above I work through each item but how do I find the name when I have an input or textarea tag in my grasp?!
Many thanks!
I also tried implementing it inside a loop and evaluated each element to find an input or textarea tag. Is there a way of retireving the whole tag and contents as a string from the IHTMLElement? get_innerHTML only gets the text from inside the textarea tag. If it's any help, my code is shown below.
IHTMLDocument2 *pHtmlDoc = NULL;
if (SUCCEEDED(GetDHtmlDocumen
{
IHTMLElementCollection* pColl = NULL;
if (SUCCEEDED(pHtmlDoc->get_a
{
long nLength = 0;
pColl->get_length(&nLength
for (long nCount = 0; nCount < nLength; nCount++)
{
COleVariant vIdx(nCount, VT_I4);
IDispatch* pElemDispatch = NULL;
IHTMLElement * pElem = NULL;
if (SUCCEEDED(pColl->item(vId
&& (pElemDispatch != NULL))
{
if (SUCCEEDED(pElemDispatch->
(void**)&pElem)) && (pElem != NULL))
{
BSTR bstrTagName;
CString sTempTagName;
if (!FAILED(pElem->get_tagNam
{
sTempTagName = bstrTagName;
sTempTagName.MakeLower();
SysFreeString(bstrTagName)
}
if ((sTempTagName == _T("input")) || (sTempTagName == _T("textarea")))
{
BSTR bstrText;
pElem->get_innerHTML(&bstr
SysFreeString(bstrText);
}
pElem->Release();
}
pElemDispatch->Release();
}
}
pColl->Release();
}
pHtmlDoc->Release();
}
Secondly, Dan, I'm writing the HTML for the program so all <input> or <textarea> tags must have a name. I only use ID's if I need to detect a click on that item. As shown above I work through each item but how do I find the name when I have an input or textarea tag in my grasp?!
Many thanks!
ASKER
I've come across getAttribute (don't know why I didn't notice it earlier) which should be exactly what I want but I'm having trouble using it. The VARIANT data type is not something I'm used to so perhaps I'm using it wrong. I had to include "comutil.h" which I assume is that correct header file.
Any ideas why varInputValue.bstrVal is NULL (0xcccccccc)?
VARIANT varInputName;
if (SUCCEEDED(pElem->getAttri bute((BSTR )(_T("name ")),0,&var InputName) ) && varInputName.bstrVal)
{
VARIANT varInputValue;
if (SUCCEEDED(pElem->getAttri bute((BSTR )(_T("valu e")),0,&va rInputValu e)) && varInputValue.bstrVal)
{
bstrText = varInputValue.bstrVal;
}
}
The source code I found used _variant_t and _bstr_t instead of VARIANT and (BSTR) but they gave me the following compile error:
LNK2019: unresolved external symbol "void __stdcall _com_issue_error(long)" (?_com_issue_error@@YGXJ@Z ) referenced in function "public: __thiscall _bstr_t::_bstr_t(char const *)" (??0_bstr_t@@QAE@PBD@Z)
Thanks once again for your help.
Any ideas why varInputValue.bstrVal is NULL (0xcccccccc)?
VARIANT varInputName;
if (SUCCEEDED(pElem->getAttri
{
VARIANT varInputValue;
if (SUCCEEDED(pElem->getAttri
{
bstrText = varInputValue.bstrVal;
}
}
The source code I found used _variant_t and _bstr_t instead of VARIANT and (BSTR) but they gave me the following compile error:
LNK2019: unresolved external symbol "void __stdcall _com_issue_error(long)" (?_com_issue_error@@YGXJ@Z
Thanks once again for your help.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cheers for the code Dan. If I put it directly in my SaveUserInput function it’s not a happy bunny! I guess because I’m using CDHtmlDialog and not hosting a WebBrowser control. Is it possible to stick with what I have? I’m sure the code I already have will work with a bit more tweaking.
Do I have to initialise the VARIANT variable or include a difference header file?
Thanks.
Do I have to initialise the VARIANT variable or include a difference header file?
Thanks.
You should be able to make just one change... to get the pointer to the document (I don't know HTML Dialogs at all, but there is *always* a way to get that).
My guess is that it will involve a call to
GetDHtmlDocument()
>>Do I have to initialise the VARIANT variable or include a difference header file?
You should just use
_variant_t
and
_bstr_t
objects. They simplify all access to these COM datatypes; for instance, they free the associated memory when they destruct at the end of the scope.
-- Dan
My guess is that it will involve a call to
GetDHtmlDocument()
>>Do I have to initialise the VARIANT variable or include a difference header file?
You should just use
_variant_t
and
_bstr_t
objects. They simplify all access to these COM datatypes; for instance, they free the associated memory when they destruct at the end of the scope.
-- Dan
ASKER
Thanks but I think I've cracked it! It was all to do with:
pElem->getAttribute((BSTR) (_T("name" )),0,&varI nputName)
which should have been:
pElem->getAttribute(L"name ",0,&varIn putName)
Just what do the L and the _T do anyway? Just out of interest, why did I get unresolved external symbol errors when I used _variant_t and _bstr_t instead of VARIANT and BSTR?
Many thanks for you time and effort. Well worth some points anyway.
pElem->getAttribute((BSTR)
which should have been:
pElem->getAttribute(L"name
Just what do the L and the _T do anyway? Just out of interest, why did I get unresolved external symbol errors when I used _variant_t and _bstr_t instead of VARIANT and BSTR?
Many thanks for you time and effort. Well worth some points anyway.
I don't know why you would get about unresolved externals. The headers are in
#include <comdef.h>
which also pulls in several libraries...
#pragma comment(lib, "comsupp.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
They may have changed something in the MFC 7 (aka VC++.Net)
>>Just what do the L and the _T do anyway?
L"xxxx" forces the compiler to store the string literal internally as UNICODE (16-bit wide) characters. All COM interfaces expect 16-bit wide characters.
_T("xxxxx") forces the compiler to store the string literal as either UNICODE or 8-bit characters, depending upon whether you have defined UNICODE or not in your preprocessor settings.
Given that
pElem->getAttribute((BSTR) (_T("name" )),0,&varI nputName)
failed but
pElem->getAttribute(L"name ",0,&varIn putName)
worked, I'd say that you are working with a non-UNICODE build.
-- Dan
#include <comdef.h>
which also pulls in several libraries...
#pragma comment(lib, "comsupp.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "oleaut32.lib")
They may have changed something in the MFC 7 (aka VC++.Net)
>>Just what do the L and the _T do anyway?
L"xxxx" forces the compiler to store the string literal internally as UNICODE (16-bit wide) characters. All COM interfaces expect 16-bit wide characters.
_T("xxxxx") forces the compiler to store the string literal as either UNICODE or 8-bit characters, depending upon whether you have defined UNICODE or not in your preprocessor settings.
Given that
pElem->getAttribute((BSTR)
failed but
pElem->getAttribute(L"name
worked, I'd say that you are working with a non-UNICODE build.
-- Dan
ASKER
Ah I see. Many thanks for the help! Much appreciated. Night night.
What you should do is cycle through all of the elements by getting the IHTMLElementCollection of the document (or maybe just document.all) and working through each item. Each item will be an IHTMLElement and you can use the .tagName to see if it is an input or textarea.
-- Dan