I want to automate the following process: I log in to a website. On the search page, enter the information I want to search and click on the search button. The click loads my result. From the resulting page, I select the option to preview the pdf file, and click the button. This click uploads my pdf file.
If I split these commands into buttons, and I press it one by one everything is working fine, but when I want to execute all the commands in a row I get the an error. (I think the error is because WebBrowser does not load everything.) Please help.
procedure TMainForm.Button10Click(Sender: TObject);
var doc :IHTMLDocument2;
FileStream: TFileStream;
StreamAdapter: IStream;
PersistStreamInit: IPersistStreamInit;
i, a, b : integer;
sir: string;
MergiInainte:boolean;'
begin
MergiInainte:=false;
WebBrowser1.Navigate('https://website/externallogin.aspx');'
repeat
if ((WebBrowser1.ReadyState = READYSTATE_COMPLETE) and (WebBrowser1.Busy = false)) then
MergiInainte:=true;
until MergiInainte;' // This repeat is not working. Dont know why?
//After webBrowser is fully loaded, the next code has to be run
doc := WebBrowser1.Document AS IHTMLDocument2;
WebFormSetFieldValue(doc,0,'txtNume','user');
WebFormSetFieldValue(doc,0,'txtPwd','psswd');
WebBrowser1.OleObject.Document.GetElementByID('tibAutentification').click;'
//after the above code is fully executat, next code is in line
WebBrowser1.Navigate('https://website/Pages/SearchTitle.aspx');
//after the above code is executed, next i need to complete form element with what the nexd data
doc := WebBrowser1.Document AS IHTMLDocument2;
WebBrowser1.OleObject.Document.GetElementByID('chkTitleNo').click;
WebFormSetFieldValue(doc,0,'txtTitleNo','28972'); //nr titlu de proprietate
WebBrowser1.OleObject.Document.GetElementByID('tibSearch').click;
WebBrowser1.OleObject.Document.GetElementByID('fdgMain:_ctl3:imbView').click;
WebBrowser1.OleObject.Document.GetElementByID('tibViewPDF').click;
end;
// But i'm getting error: Access violation....read of adress 0000000.'
procedure TForm1.WebFormSetFieldValue(const document: IHTMLDocument2; const formNumber: integer; const fieldName, newValue: string) ;
var form : IHTMLFormElement;
field: IHTMLElement;
begin
form := WebFormGet(formNumber, WebBrowser1.Document AS IHTMLDocument2) ;
field := form.Item(fieldName,'') as IHTMLElement;
if field = nil then Exit;
if field.tagName = 'INPUT' then (field as IHTMLInputElement).value := newValue;
if field.tagName = 'SELECT' then (field as IHTMLSelectElement).value := newValue;
if field.tagName = 'TEXTAREA' then (field as IHTMLTextAreaElement).value := newValue;
end;