我试图从使用twebrowser的旧代码迁移到新的tedgebrowser,但是edgebrowser没有相同的属性,所以我不能再使用我的旧函数了。
我正在使用我在这里得到的函数:GetElementByClass?
function GetInnersByClass(const Doc: IDispatch; const classname: string;var Lst:TStringList):Integer;
var
Document: IHTMLDocument2; // IHTMLDocument2 interface of Doc
Body: IHTMLElement2; // document body element
Tags: IHTMLElementCollection; // all tags in document body
Tag: IHTMLElement; // a tag in document body
I: Integer; // loops thru tags in document body
begin
Lst.Clear;
Result := 0 ;
// Check for valid document: require IHTMLDocument2 interface to it
if not Supports(Doc, IHTMLDocument2, Document) then
raise Exception.Create('Invalid HTML document');
// Check for valid body element: require IHTMLElement2 interface to it
if not Supports(Document.body, IHTMLElement2, Body) then
raise Exception.Create('Can''t find <body> element');
// Get all tags in body element ('*' => any tag name)
Tags := Body.getElementsByTagName('*');
// Scan through all tags in body
for I := 0 to Pred(Tags.length) do
begin
// Get reference to a tag
Tag := Tags.item(I, EmptyParam) as IHTMLElement;
// Check tag's id and return it if id matches
if AnsiSameText(Tag.className, classname) then
begin
Lst.Add(Tag.innerHTML);
Inc(Result);
end;
end;
end;
然后,例如,我将其命名为: GetInnersByClass(WebBrowser1.Document,‘类名’,lst);
我将“类名”的内部文本输入变量lst。
但是TEdgeBrowser没有Document属性。
它不一定是相同的功能。我需要的是从加载在TEdgeBrowser中的元素中获取内部文本。
有没有人有什么想法怎么做?
谢谢
发布于 2022-06-10 05:36:43
查看这篇关于Embarcadero 博客的文章。
您必须运行脚本并将结果传递给Delphi。
编辑:
请检查此演示程序(您也可以在GitHub上找到它),GitHub Pascal\VCL\WebBrowser\Edge
并添加如下代码:
procedure TfrmMain.ToolButton1Click(Sender: TObject);
begin
var LS := TFile.ReadAllText('EdgeScript.js');
EdgeBrowser.ExecuteScript(LS);
end;
EdgeScript.js:
var set = document.querySelectorAll("a.gb_d");
var values = [];
for (const elem of set) {
values.push(elem.href);
}
encodeURI(JSON.stringify({length: set.length, values: values}));_
导航到google.com并运行脚本。这是我的代码,用于查找所有具有指定类名的锚。修改javacript以查找元素并返回InnerText。
https://stackoverflow.com/questions/72568143
复制相似问题