如何使用 CppWebBrowser 和 C++Builder 触发网页搜索框

How to trigger a webpage search box using CppWebBrowser and C++Builder

我在 VCL 32 位平台上使用 C++Builder XE4-pro。我正在使用名为 CppWebBrowser 的组件来加载 JPL 网页。我在下面添加了网页图片。下面的代码是我加载网页的按钮事件。该网页是通过在搜索框中手动输入小行星名称并按回车键来使用的。此 运行 是一个加载小行星详细信息的脚本。我需要使用代码 运行 搜索框。一个样本小行星名称是 Eros。有什么方法可以使用代码激活这个搜索框吗?

void __fastcall TForm1::Button1Click(TObject *Sender)
{
UnicodeString Full;
Full = "https://ssd.jpl.nasa.gov/sbdb.cgi#top";
CppWebBrowser1->Navigate( Full.c_str() );
}

页面加载完成后(触发OnDocumentComplete事件),可以使用浏览器自带的DOM接口查找并填写搜索框,然后提交包含表格。

首先查询 Document 属性 的 IHTMLDocument2 接口,然后从那里开始。

IHTMLDocument2有一个forms collection you can use to enumerate the <form> elements on the page, enumerating each form's <input> elements using the IHTMLDocument3::item() method until you find the <input> element of the desired search box. Or, you can query the Document for IHTMLDocument3 and use its getElementById() or GetElementsByName()方法可以直接找到想要的搜索框。

找到搜索元素后,可以使用 IHTMLInputElement, set its value property to whatever text you want, and then call its containing form's submit() 方法对其进行查询。