随机使用 IHTMLDocument3 getElementsByTagName returns 无

getElementsByTagName with IHTMLDocument3 randomly returns nothing

我正在尝试通过 C++ 程序在 Internet Explorer 中填写一些表单输入字段,但我遇到了一个随机错误,我希望这是因为我的代码:

UINT msg = RegisterWindowMessage("WM_HTML_GETOBJECT");
LRESULT result = 0;
SendMessageTimeout(hwnd, msg, NULL, NULL, SMTO_ABORTIFHUNG, 10000, (PDWORD_PTR)&result);
if (!result)
    return;


// get main document object
IHTMLDocument3 *doc = NULL;
ObjectFromLresult(result, IID_IHTMLDocument3, NULL, (void**)&doc);
if (!doc)
    return;

VARIANT varint, varstr;
varint.vt = VT_I4;
varstr.vt = VT_BSTR;


IHTMLElementCollection* pElemCollections=NULL;


if (FAILED(doc->getElementsByTagName(L"input", &pElemCollections)))
    return;

long nelm;
pElemCollections->get_length(&nelm);

...

在最后一行并在同一页上使用相同的 HWND,我有时会得到正确的数字或​​输入字段,并且经常得到 0 的 nelm。

您是否发现我的代码有问题或者是错误? 请注意,我验证了 HWND 是正确的,并且从未调用 return

谢谢

这样做我就没有问题了:

UINT msg = RegisterWindowMessage("WM_HTML_GETOBJECT");
LRESULT result = 0;
SendMessageTimeout(hwnd, msg, NULL, NULL, SMTO_ABORTIFHUNG, 10000, (PDWORD_PTR)&result);
if (!result)
    return;


// get main document object
IHTMLDocument3 *doc = NULL;
ObjectFromLresult(result, IID_IHTMLDocument3, NULL, (void**)&doc);
if (!doc)
    return;

CComVariant varint;
CComVariant varstr;


IHTMLElementCollection* pElemCollections=NULL;

CComBSTR name(L"input")
if (FAILED(doc->getElementsByTagName(name, &pElemCollections)))
    return;

long nelm;
pElemCollections->get_length(&nelm);

...