Delphi Coding - Delphide Webbrowser Usage

Delphi Coding - Delphide Webbrowser Usage

Nowadays, many processes are performed on web pages and you can check web pages with delphi coding. However, in cases such as assigning passwords to users, it is necessary to do the same on the website many times. With Delphi, we can automate the process with webbrowser.

The web browser component in Delphi 7 is actually an ActiveX component of the version of Internet Explorer on the operating system. This component is on the Internet tab in Delphi 7.

The Webbrowser component works like Internet Explorer. So if you have an older version of Internet Explorer, it's possible that you'll get error messages from scripts on current sites. In this case, we use the following code to prevent the Delphi side from interrupting the transaction:

WebBrowser1.Silent:=true;

The structure we use to open the web address entered in the component is as follows:

Here we use the Navigate method to specify the address of the web page to be visited. To wait for the entire web page to load, the while loop is being used.

WebBrowser1.Navigate(edtUrl.Text);
while WebBrowser1.ReadyState <> READYSTATE_COMPLETE do begin
Sleep(1);
Application.ProcessMessages;
end;

While automating operations, we will probably need to log in to the web page. Therefore, we need to be able to automatically fill in the input sections on the web page. To do this, we use the following structure:

WebBrowser1.OleObject.Document.GetElementByID('username').Value := 'admin';
WebBrowser1.OleObject.Document.GetElementByID('password').Value := '123456';

In the above example, the user name and password to input the id of the input page by looking at the source code of the username and password are detected.

Things are getting a little confused after this point. Because the web page form to submit the submit button they can create different types. Let us first examine the following structure:

"<input value="GÖNDER" type="submit">"

As seen in this structure, there is no id of the button. We only have SEND value in value. Here we use the following subprograms for Delphi to provide these types of buttons:

procedure WB_send_Click_by_Value(WB: TWebbrowser;form_nr:nativeint;tag,typ,val: string);
var ovElements: OleVariant;
i: Integer;
begin
ovElements := WB.OleObject.Document.forms.item(form_nr).elements;
for i := 0 to (ovElements.Length - 1) do
begin
if AnsiSameText(ovElements.item(i).tagName,tag) and
AnsiSameText(ovElements.item(i).type,typ) and
AnsiSameText(ovElements.item(i).value,val) then
ovElements.item(i).Click;
end;
end;

In order to click the SEND button in the example above, we call this subprogram as follows:

WB_send_Click_by_Value(WebBrowser1,0,'input','submit','GÖNDER');

A variant of the Submit button can also be encoded as follows:

"<button type="submit">Gönder</button>"

In this structure we use the following codes to click the button.

var
Elem: IHTMLElement;
Buttons: OleVariant;
Button: OleVariant;
I: Integer;
begin
Buttons := WebBrowser1.OleObject.Document.getElementsByTagName('button');
for I := 0 to Buttons.Length - 1 do
begin
Button := Buttons.item(I);
if Button.innerText = 'Gönder' then
begin
Button.click();
Break;
end;
end;

After that, our job is to take the information we want from the page to the Delphi program. We are having trouble getting the values returned here with Ajax. To get rid of this problem, the subprogram is:

function GetElementById(const Doc: IDispatch; const Id: string): IDispatch;
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
Result := nil;
// 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 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.id, Id) then
begin
Result := Tag;
Break;
end;
end;
end;

With the help of the above subprogram, we are able to reflect the value returned by Ajax to ShowMessage as follows:

procedure TForm1.Button1Click(Sender: TObject);
var
Elem: IHTMLElement;
begin
Elem := GetElementById(WebBrowser1.Document, 'Ajaxla-Donen-Id') as IHTMLElement;
if Assigned(Elem) then
ShowMessage(
'Tag name = <' + Elem.tagName + '>'#10 +
'Tag id = ' + Elem.id + #10 +
'Tag innerHTML = "' + Elem.innerHTML + '"'
);
end;