分享

Delphi TWebBrowser[6] 获取网页所有链接(元素)、下拉菜单及GetElementByID返回值的有效性判定方法

 hncdman 2022-10-31 发布于湖南

1、获取网页所有链接

var

  elem: IHTMLElement;

  coll: IHTMLElementCollection;

  i: integer;

  url, title: string;

begin

  coll := (WebBrowser1.Document as IHTMLDocument2).all;

  coll := (coll.tags('a') as IHTMLElementCollection);

  for i := 0 to coll.Length - 1 do

  begin //   循环取出每个链接

    elem := (coll.item(i, 0) as IHTMLElement);

    url := Trim(string(elem.getAttribute(WideString('href'), 0)));

    title := elem.innerText;

    ShowMessage(Format('链接标题:%s,链接网址:%s', [title, url]));

  end;

end;


其他元素的获取,方法类似

-----------------------------------

Delphi TWebBrowser[6] 获取网页所有链接(元素)、下拉菜单及GetElementByID返回值的有效性判定方法

https://blog.51cto.com/u_15069471/4024964

下拉菜单

uses MsHtml;

var

  doc: IHTMLDocument2;

  coll: IHTMLElementCollection;

  iPos, iIndex: Integer;

  selElem: IHtmlSelectElement;

  optElem: IHtmlOptionElement;

begin

  doc := WebBrowser1.Document as IHTMLDocument2;

  if doc = nil then Exit;

  coll := doc.all.tags('select') as IHTMLElementCollection;

  iPos := 0; //要访问的下拉菜单的序号,从0开始为第一个

  selElem := coll.item(iPos, 0) as IHtmlSelectElement;

  if selElem = nil then Exit;

  iIndex := 2; //下拉菜单的选项序号,从0开始为第一个,2为第三个选项

  optElem := selElem.item(iIndex, 0) as IHtmlOptionElement;

  if optElem = nil then Exit;

  ShowMessage(optElem.text); //获取该选项的值

  optElem.selected := True;  //选中该选项

end; 

-----------------------------------

Delphi TWebBrowser[6] 获取网页所有链接(元素)、下拉菜单及GetElementByID返回值的有效性判定方法

https://blog.51cto.com/u_15069471/4024964

GetElementByID返回值有效性判定方法

var

  aElement: OleVariant;

begin

  aElement := WebBrowser1.OleObject.Document.GetElementByID('btnLogin');

  if IDispatch(aElement) <> nil then //对返回值进行有效性检查

  begin

    aElement.value := '登录按钮';

    aElement.click;

  end;

end;

-----------------------------------

Delphi TWebBrowser[6] 获取网页所有链接(元素)、下拉菜单及GetElementByID返回值的有效性判定方法

https://blog.51cto.com/u_15069471/4024964

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多