首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >1如何使用Delphi10.4中的新InnerText从类名元素中获取TEdgeBrowser?

1如何使用Delphi10.4中的新InnerText从类名元素中获取TEdgeBrowser?
EN

Stack Overflow用户
提问于 2022-06-10 01:14:59
回答 1查看 213关注 0票数 0

我试图从使用twebrowser的旧代码迁移到新的tedgebrowser,但是edgebrowser没有相同的属性,所以我不能再使用我的旧函数了。

我正在使用我在这里得到的函数:GetElementByClass?

代码语言:javascript
运行
复制
  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中的元素中获取内部文本。

有没有人有什么想法怎么做?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2022-06-10 05:36:43

查看这篇关于Embarcadero 博客的文章。

您必须运行脚本并将结果传递给Delphi。

编辑:

请检查此演示程序(您也可以在GitHub上找到它),GitHub Pascal\VCL\WebBrowser\Edge

并添加如下代码:

代码语言:javascript
运行
复制
procedure TfrmMain.ToolButton1Click(Sender: TObject);
begin
  var LS := TFile.ReadAllText('EdgeScript.js');
  EdgeBrowser.ExecuteScript(LS);
end;

EdgeScript.js:

代码语言:javascript
运行
复制
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。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72568143

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档