要从 TChromium 实例获取嵌入在当前 Web 文档中的特定 DOM 节点,使用其 ID,您可以使用 ICefDomDocument.getElementById()。但是如何通过 NAME 属性找到元素呢?Javascript 有 document.getElementsByName() 方法和 TWebBrowser(包装 IE)有一个类似的调用,但我不知道如何用 TChromium 做到这一点。我需要找到一些具有 NAME 属性但没有 ID 属性的 DOM 元素。我搜索了ceflib单元,没有看到任何可以做到的东西。
边问。如果有人有 TChromium“食谱”风格网站或文档的链接,我可以使用它。
更新:在等待答案时,我想出了以下代码来执行 getElementsbyName()。我想要比扫描整个 DOM 树更快的东西。如果您在代码中发现有问题,请告诉我:
type
TDynamicCefDomNodeArray = array of ICefDomNode;
// Given a Chromium document interface reference and a NAME attribute to search for,
// return an array of all DOM nodes whose NAME attribute matches the desired.
function getElementsByName(ADocument: ICefDomDocument; theName: string): TDynamicCefDomNodeArray;
// Get all the elements with a particular NAME attribute value and return
// an array of them.
procedure getElementsByName1(intfParentNode: ICefDomNode; theName: string; var aryResults: TDynamicCefDomNodeArray);
var
oldLen: integer;
intfChildNode: ICefDomNode;
theNameAttr: string;
begin
Result := nil;
intfChildNode := nil;
if Assigned(intfParentNode) then
begin
// Attributes are case insensitive.
theNameAttr := intfParentNode.GetElementAttribute('name');
if AnsiSameText(theNameAttr, theName) then
begin
// Name attribute match. Add it to the results array.
oldLen := Length(aryResults);
SetLength(aryResults, oldLen + 1);
aryResults[oldLen] := intfParentNode;
end; // if AnsiSameText(intfParentNode.Name, theName) then
// Does the parent node have children?
if intfParentNode.HasChildren then
begin
intfChildNode := intfParentNode.FirstChild;
// Scan them.
while Assigned(intfChildNode) do
begin
getElementsByName1(intfChildNode, theName, aryResults);
if Assigned(intfChildNode) then
intfChildNode := intfChildNode.NextSibling;
end;
end; // if intfParentNode.HasChildren then
end; // if Assigned(intfParentNode) then
end;
// ---------------------------------------------------------------
var
intfCefDomNode: ICefDomNode;
begin
intfCefDomNode := nil;
Result := nil;
if Assigned(ADocument) then
begin
// Check the header.
intfCefDomNode := ADocument.Document;
if Assigned(intfCefDomNode) then
begin
// Check the parent.
getElementsByName1(intfCefDomNode, theName, Result);
end; // if Assigned(intfCefDomNode) then
end; // if Assigned(ADocoument) then
end;
// ---------------------------------------------------------------