Novo

Forum kao aplikacija na telefonu

Dodajte forum na početni ekran i koristite ga kao aplikaciju, sa menijem na dnu ekrana.

  • 30 odgovora
  • 7.277 pregleda

Najnovija ↓

Igranje s TWebBrowserom 03.11.2007. u 14:19 · pre 231 mesec #1
Koristim TWebBrowser kako bih popunio neku formu i submitovao je. Problem se javlja sto kad uradim submit izbaci neki warning tipa: document.forms.0.email is null or not an object. I pita da li da nastavi sa scriptom ili da je ugasi. Zasto se ovo desava, i moze li se nekako iskljuciti? Evo koda:

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw, StdCtrls, mshtml, ActiveX;

type
  TForm1 = class(TForm)
    Button1: TButton;
    WebBrowser1: TWebBrowser;
    Button2: TButton;
    procedure Button2Click(Sender: TObject);
    procedure WebBrowser1NavigateComplete2(Sender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
    procedure WebBrowser1DocumentComplete(Sender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
    function GetFormByNumber(document: IHTMLDocument2;
    formNumber: integer): IHTMLFormElement;
    procedure SetFieldValue(theForm: IHTMLFormElement;
    const fieldName: string; const newValue: string);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;


var
  Form1: TForm1;
  CurDispatch: IDispatch;
 implementation

{$R *.dfm}


function tform1.GetFormByNumber(document: IHTMLDocument2;
    formNumber: integer): IHTMLFormElement;
var
  forms: IHTMLElementCollection;
begin
  forms := document.Forms as IHTMLElementCollection;
  if formNumber < forms.Length then
    result := forms.Item(formNumber,'') as IHTMLFormElement
  else
    result := nil;
end;

procedure tform1.SetFieldValue(theForm: IHTMLFormElement;
    const fieldName: string; const newValue: string);
var
  field: IHTMLElement;
  inputField: IHTMLInputElement;
  selectField: IHTMLSelectElement;
  textField: IHTMLTextAreaElement;
begin
  field := theForm.Item(fieldName,'') as IHTMLElement;
  if Assigned(field) then
  begin
    if field.tagName = 'INPUT' then
    begin
      inputField := field as IHTMLInputElement;
      inputField.value := newValue;
    end
    else if field.tagName = 'SELECT' then
    begin
      selectField := field as IHTMLSelectElement;
      selectField.value := newValue;
    end
    else if field.tagName = 'TEXTAREA' then
    begin
      textField := field as IHTMLTextAreaElement;
      textField.value := newValue;
    end;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
WebBrowser1.Navigate('www.nekisajt.com');
end;

procedure TForm1.WebBrowser1NavigateComplete2(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
  if CurDispatch = nil then
    CurDispatch := pDisp;
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
 if (pDisp = CurDispatch) then
  begin
  //desavanja pri zavrsetku ucitavanja sajta
  CurDispatch:= nil;
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  document: IHTMLDocument2;
  theForm: IHTMLFormElement;
  index: integer;
begin
  document := WebBrowser1.Document as IHTMLDocument2;
  theForm := GetFormByNumber(WebBrowser1.Document as IHTMLDocument2,0);
  SetFieldValue(theForm,'email','[email protected]');
  SetFieldValue(theForm,'password','password');
  theForm.submit;
end;

end.


Cudna stvar se takodje desava kad pokusam da ovo popunjavanje forme i submitovanje stavim da se desava pri zavrsetku ucitavanja sajta. Dobijem neki AccessViolation 0000000, i nikako ne mogu da skapiram u cemu je problem...
Re: Igranje s TWebBrowserom 04.11.2007. u 00:22 · pre 231 mesec #2
meni radi na mom primeru. daj tacno koji je to sajt da probamo...
Re: Igranje s TWebBrowserom 04.11.2007. u 13:22 · pre 231 mesec #4
Ja imam slican problem, ovako radi, ali je problem kad ima veci broj formi u HTML dokumentu i polja koja treba popuniti.

Npr. ja umesto funkije koja je u gornjem primeru GetFormByNumber koristim GetFormByName (mislim tako mi je bolje da ne pogresim sta gde treba da unesem)
Code:
function GetFormByName(document: IHTMLDocument2;
    const formName: string): IHTMLFormElement;
var
  forms: IHTMLElementCollection;
begin
  forms := document.Forms as IHTMLElementCollection;
  result := forms.Item(formName,'') as IHTMLFormElement
end;


Tekst koji zelim mi lepo prikaze da je popunio u zeljeno polje
Code:

procedure TForm1.Button1Click(Sender: TObject);
var
  document: IHTMLDocument2;
  theForm: IHTMLFormElement;
  index: integer;
begin
  document := WebBrowser1.Document as IHTMLDocument2;
  theForm := GetFormByName(WebBrowser1.Document as IHTMLDocument2,'myForm');
  theForm := GetFormByName(WebBrowser1.Document as IHTMLDocument2,'chatForm');
  SetFieldValue(theForm,'chatbarText','Proba za unos automatskog teksta...');
  theForm.submit;
end;


I kad da posalje taj tekst koji je uneo u polje nista se ne desi?!

Zasto?
Potpis

My IT View Of Life-BrLOG-Delphi for PHP


» LinkedIn
Re: Igranje s TWebBrowserom 04.11.2007. u 13:33 · pre 231 mesec #5
stavis negde WebBrowser1.Navigate('http://www.yuwie.com/login.asp');


Code:

uses  ... MSHTML;

...

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  Doc: IHTMLDocument2;
  HTMLWindow: IHTMLWindow2;
begin
  Doc := WebBrowser1.Document as IHTMLDocument2;
  if not Assigned(Doc) then
    Exit;
  HTMLWindow := Doc.parentWindow;
  if not Assigned(HTMLWindow) then
    Exit;
  try
    HTMLWindow.execScript('document.forms[0].email.value="[email protected]";', 'JavaScript');
    HTMLWindow.execScript('document.forms[0].password.value="sifra";', 'JavaScript');
    HTMLWindow.execScript('document.forms[0].submit();', 'JavaScript');
  except
    showmessage('greska!');
  end;
end;




javascript pomaze :)

mozes i sa document.getElementById("id_od_polja") da mu pristupis ako je zadan id, ako nije onda document.naziv_za_formular.naziv_za_polje ili ako form nema naziv onda po broju kao u primeru.
samo moras dodati negde da ti prepozna da li je ucitana stranica za login ili neka druga da ne radi svaki put ovo
Re: Igranje s TWebBrowserom 04.11.2007. u 15:35 · pre 231 mesec #7
to je javascript

ako imas u html-u npr
Code:

<form name=frm action=sajt.php>
<input name=tekst value="default vrednost" id=tekst>
</form>


onda tom objektu na stranici mozes zadati value:
Code:

document.getElementById("tekst").value = "nova vrednost";

ili
Code:

document.frm.tekst.value = "nova vrednost";

ili ako je to prvi form na stranici i nema ime niti ID, kao sto je onamo slucaj:
Code:

document.forms[0].tekst.value = "nova vrednost";


document.all nije preporucljivo koristiti...
Re: Igranje s TWebBrowserom 04.11.2007. u 16:28 · pre 231 mesec #9
Evo probao sam preko JavaScripta da popunim formu, ali opet ista greska, samo sto sada bilo da kliknem Yes ili No ono nece da radi vec podigne neki Exception... pogledajte:



i onda ja clicknem Yes i dobijem:



a koristio sam onaj cod koji je _deran_ prilozio:

Code:

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  Doc: IHTMLDocument2;
  HTMLWindow: IHTMLWindow2;
begin
  Doc := WebBrowser1.Document as IHTMLDocument2;
  if not Assigned(Doc) then
    Exit;
  HTMLWindow := Doc.parentWindow;
  if not Assigned(HTMLWindow) then
    Exit;
  try
    HTMLWindow.execScript('document.forms[0].email.value="[email protected]";', 'JavaScript');
    HTMLWindow.execScript('document.forms[0].password.value="sifra";', 'JavaScript');
    HTMLWindow.execScript('document.forms[0].submit();', 'JavaScript');
  except
    showmessage('greska!');
  end;
end;


Jos jedna cudna stvar je sto se ovo desava samo kad unesem tacan email i password, a kada su pogresni samo mi prikaze da taj email ne postoji u njihovoj bazi ili sta vec.
Re: Igranje s TWebBrowserom 04.11.2007. u 17:46 · pre 231 mesec #10
@VladaLe
napravis proceduru, recimo:

Code:

procedure TForm1.startuj_script(script: string);
var
  Doc: IHTMLDocument2;
  HTMLWindow: IHTMLWindow2;
begin
  Doc := WebBrowser1.Document as IHTMLDocument2;
  if not Assigned(Doc) then
    Exit;
  HTMLWindow := Doc.parentWindow;
  if not Assigned(HTMLWindow) then
    Exit;
  try
    HTMLWindow.execScript(script, 'JavaScript');
  except
  end;
end;


i onda mozes na onclick nekog button-a da stavis
Code:

startuj_script('document.frm.tekst.value = "nova vrednost";');

ali vodi racuna o tome da na toj stranici onda postoji taj objekat, tj da je stranica ucitana. ili jos bolje, u javascript-u napravi da ako postoji taj objekat neka mu promeni value:
Code:

startuj_script('if (document.frm.tekst) { document.frm.tekst.value = "nova vrednost"; }');


ako imas vise twebbrowser-a mozes kao parametar preneti u kojem da se izvrsava...

@krza
koji Exception?
da li ga izbaci delphi ili taj sajt?
probaj taj isti nalog da otvoris u IE
nemam otvoren nalog tamo pa ne mogu da probam...
Re: Igranje s TWebBrowserom 04.11.2007. u 18:17 · pre 231 mesec #12
postavio sam image-a samo su linkovi filtrirani...

Anyway, gresku prijavljuje samo kad se ulogujem preko TWebBrowsera, ako koristim IE da se ulogujem, nema nikakvih errora. Probao sam takodje i u IE preko javascripte da popunim polja i da submitujem umesto da clicknem, ali opet nema nikakvog errora, znaci mora da je nesto vezano za TWebBrowser komponentu jer mi se to desavalo i ranije koliko se secam....
Re: Igranje s TWebBrowserom 04.11.2007. u 19:02 · pre 231 mesec #15
treba "document.forms[0].email.value" a ne "document.forms.0.email.value" 0 nije naziv nego redni broj, ali bilo bi bolje gde moze da se koristi naziv ili ID ako je zadan. prepravljao si primer koji sam dao?
Re: Igranje s TWebBrowserom 04.11.2007. u 19:07 · pre 231 mesec #16
Probao sam da upgrade-ujem na IE i na trenutak sam pomislio da radi, ali jok. _deran_, nisam menjao kod, on sam iz nekog razloga ispisuje forms.0. umesto forms[0] kad prijavi tu gresku.
Re: Igranje s TWebBrowserom 04.11.2007. u 19:10 · pre 231 mesec #17
Hmm, cini se da nemam ovaj problem ako koristim TEmbeddedWB komponentu. Izgleda da je problem bio u TWebBrowser.

Hvala na pomoci.
Re: Igranje s TWebBrowserom 04.11.2007. u 19:21 · pre 231 mesec #18
Grrrr ovo stvarno ume da iznervira.
Evo sad sam prebacio taj kod u ovaj program koji radim, zamenio TWebBrowser sa TEmbeddedWB i u onDocumentComplete stavio:

procedure TForm1.Embeddedwb1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
Doc: IHTMLDocument2;
HTMLWindow: IHTMLWindow2;
begin
if CurDispatch=pdisp then
begin
Doc := embeddedwb1.Document as IHTMLDocument2;
if not Assigned(Doc) then
Exit;
HTMLWindow := Doc.parentWindow;
if not Assigned(HTMLWindow) then
Exit;
HTMLWindow.execScript('document.forms[0].email.value = "****";', 'JavaScript');
HTMLWindow.execScript('document.forms[0].password.value = "****";', 'JavaScript');
Embeddedwb1.OleObject.document.forms.item(0).elements.item(5).click;
// HTMLWindow.execScript('document.forms[0].submit();', 'JavaScript');
end;
end;

I kad dodje do onog dela da treba da clickne, odnosno da submituje formu podigne EOleException 80020101 !!!! Sta mu je vise?!
Re: Igranje s TWebBrowserom 04.11.2007. u 19:33 · pre 231 mesec
izmenjeno 1 put, poslednji put krza 04.11.2007. u 20:03
  1. krza04.11.2007. u 20:03zapis iz teksta poruke
#19
cetvrti post u nizu :)
Resio sam i ovaj problem :), stvar je bila u tome sto nisam odredio pri kom URLu da ispunjava ovu formu pa je zabrljavio kad se ulogovao i pokusao i u tom urlu da ispuni formu koja sad ne postoji. Kod izgleda ovako nekako:

definisao sam "login : integer" i form1.oncreate stavio login:=0;

zatim:

procedure TForm1.EmbeddedWB1DocumentComplete(ASender: TObject;
const pDisp: IDispatch; var URL: OleVariant);
var
Doc: IHTMLDocument2;
HTMLWindow: IHTMLWindow2;
begin
Doc := embeddedwb1.Document as IHTMLDocument2;
if not Assigned(Doc) then
Exit;
HTMLWindow := Doc.parentWindow;
if not Assigned(HTMLWindow) then
Exit;
try
if (URL='http://www.yuwie.com/login.asp') and (login=0) then
begin
HTMLWindow.execScript('document.forms[0].email.value="****";', 'JavaScript');
HTMLWindow.execScript('document.forms[0].password.value="****";', 'JavaScript');
HTMLWindow.execScript('document.forms[0].submit();', 'JavaScript');
login:=1;
end;
except
showmessage('greska!');
end;
end;

i to je to. Jos jednom, hvala svima :)
Re: Igranje s TWebBrowserom 04.11.2007. u 19:51 · pre 231 mesec #20
Evo kako izgleda forma gde ja hocu da ubacim neki tekst.
Code:
<form name="chatForm" id="chatForm" onsubmit="return false;" action="">
<input value="212.200.88.252" name="name" id="name" onblur="checkName();" type="hidden">
<input maxlength="500" id="chatbarText" name="chatbarText" onblur="checkStatus('');" onfocus="checkStatus('active');" type="text">
<input onclick="sendComment();" id="submit" name="submit" value="šubmit" type="submit">
</form>

Dakle zelim da ubacim tekst u chatbarText a on je u formi chatForm i da to postujem.
Kada pokusam ovo izbaci mi gresku...
Code:

procedure TForm1.Button1Click(Sender: TObject);
var
  Doc: IHTMLDocument2;
  HTMLWindow: IHTMLWindow2;
begin
  Doc := WebBrowser1.Document as IHTMLDocument2;
  if not Assigned(Doc) then
    Exit;
  HTMLWindow := Doc.parentWindow;
  if not Assigned(HTMLWindow) then
    Exit;
  try
    HTMLWindow.execScript('document.getElementById("chatbarText").value="Testing bla bla...";', 'JavaScript');
    HTMLWindow.execScript('documentforms["chatForm"].sendComment();', 'JavaScript');
    except
    showmessage('greska!');
  end;
end;


U cemu je problem? (Tekst koji sam stavio onde gore "Testing bla bla bla...." postavi gde treba, ali nece da postuje to :()
Potpis

My IT View Of Life-BrLOG-Delphi for PHP


» LinkedIn
Odgovori
Prijavite se da biste odgovorili u ovoj temi.PrijavaRegistracija

Premeštanje teme