Delphi: Webbrowser.Document -> 通过 ID 检查元素是否存在
Delphi: Webbrowser.Document -> check existence of Element by ID
我有一个 TWebbrowser 组件和一些按钮。到目前为止效果很好,但在一个过程中,我想单击网站上的一个按钮以显示其他信息,也应该单击该按钮。
所以我得到了这个:
WebBrowser1.OleObject.Document.GetElementByID('linkDtlC0-0').Click();
WebBrowser1.OleObject.Document.GetElementByID('linkDtlC0-1').Click();
WebBrowser1.OleObject.Document.GetElementByID('linkDtlC0-2').Click();
然后我必须在网站上单击一个按钮以显示接下来要单击的三个元素。到目前为止没有问题,但在单击按钮后,网站需要几秒钟才能显示这三个元素。
我的问题:
当我尝试在清楚地单击按钮后立即单击元素时,它会导致错误,指出没有元素(还)可以使用此名称单击。当然我可以让我的程序等待几秒钟(更多)只是确定然后尝试单击,但我不想浪费时间所以我的问题是:
有没有办法检查 TWebbrowser 内的文档中是否存在元素(按名称)?
检查,元素是否存在:
uses
MSHTML;
procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; const URL: OleVariant);
var
Element: IHTMLElement;
begin
if pDisp = TWebBrowser(ASender).ControlInterface then
begin
Element := (WebBrowser1.Document as IHTMLDocument3).getElementById('linkDtlC0-3');
if Assigned(Element) then
Element.click;
end;
end;
我有一个 TWebbrowser 组件和一些按钮。到目前为止效果很好,但在一个过程中,我想单击网站上的一个按钮以显示其他信息,也应该单击该按钮。
所以我得到了这个:
WebBrowser1.OleObject.Document.GetElementByID('linkDtlC0-0').Click();
WebBrowser1.OleObject.Document.GetElementByID('linkDtlC0-1').Click();
WebBrowser1.OleObject.Document.GetElementByID('linkDtlC0-2').Click();
然后我必须在网站上单击一个按钮以显示接下来要单击的三个元素。到目前为止没有问题,但在单击按钮后,网站需要几秒钟才能显示这三个元素。
我的问题: 当我尝试在清楚地单击按钮后立即单击元素时,它会导致错误,指出没有元素(还)可以使用此名称单击。当然我可以让我的程序等待几秒钟(更多)只是确定然后尝试单击,但我不想浪费时间所以我的问题是: 有没有办法检查 TWebbrowser 内的文档中是否存在元素(按名称)?
检查,元素是否存在:
uses
MSHTML;
procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; const URL: OleVariant);
var
Element: IHTMLElement;
begin
if pDisp = TWebBrowser(ASender).ControlInterface then
begin
Element := (WebBrowser1.Document as IHTMLDocument3).getElementById('linkDtlC0-3');
if Assigned(Element) then
Element.click;
end;
end;