Einzelnen Beitrag anzeigen

API

Registriert seit: 18. Apr 2004
637 Beiträge
 
#11

AW: Login Simulieren (WebBrowser)

  Alt 11. Sep 2011, 18:01
Delphi-Quellcode:
Webbrowser1.OleObject.document.forms.item(1).elements.item('signin[username]').value := Edit1.Text;
Webbrowser1.OleObject.document.forms.item(1).elements.item('signin[password]').value := Edit2.Text;
Webbrowser1.OleObject.document.forms.item(1).Submit;
// oder: Webbrowser1.OleObject.document.forms.item(1).elements.item(3).click;
oder kürzer:

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
var
  OleObj: Variant;
begin
  OleObj := Webbrowser1.OleObject.document.forms.item(1);
  OleObj.elements.item('signin[username]').value := 'Edit1.Text';
  OleObj.elements.item('signin[password]').value := 'Edit2.Text';
  OleObj.Submit;
end;
oder "sicherer":

Delphi-Quellcode:
uses
  MSHTML;

procedure SetInputText(FormElement: IHTMLFormElement; ItemName, ItemText: string);
var
   field: IHTMLElement;
   inputField: IHTMLInputElement;
begin
   if Supports(FormElement.Item(ItemName,EmptyParam), IHTMLElement, field) then
   begin
     inputField := field as IHTMLInputElement;
     inputField.value := ItemText;
   end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  htmlDoc2: IHTMLDocument2;
  allForms: IHTMLElementCollection;
  myForm: IHTMLFormElement;
begin
  if Supports(WebBrowser1.Document, IHTMLDocument2, htmlDoc2) then
  begin
    allForms := htmlDoc2.Forms;
    if allForms.length > 1 then
    begin
      myForm := allForms.Item(1, EmptyParam) as IHTMLFormElement;
      SetInputText(myForm, 'signin[username]','username...');
      SetInputText(myForm, 'signin[password]','password...');
      myForm.submit;
    end;
  end;
end;

Geändert von API (11. Sep 2011 um 21:51 Uhr)