Delphi通过TWebBrowser浏览网页,然后访问该网页中的下拉菜单:
uses MsHtml; procedure TForm1.Button1Click(Sender: TObject); 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;
|
请发表评论