Ir ao conteúdo
  • Cadastre-se

Dúvidas - Acessando e submetendo form via código com TWebBrowser


Visitante: marilia.lima

Posts recomendados

Visitante: marilia.lima
 

Pessoal, estou tentando fazer com que um programa em delphi acesse um site via WebBrowser, passe valores de um formulário e submeta esses dados.

Só que quando eu tento buscar o HTML do site informado, está me retornando null, ou seja, nada mais funciona porque tudo depende desse html. Não sei porque está vindo null.. Alguem pode me ajudar?????

O código ficou assim:


procedure TForm1.Button1Click(Sender: TObject);
var
document: IHTMLDocument2;
theForm: IHTMLFormElement;
index: integer;
begin
WebBrowser1.Navigate('http://www.elabore.net/elabore/_form_cotacao.php');
document := WebBrowser1.document as IHTMLDocument2;
theForm := GetFormByNumber(document,0);
SetFieldValue(theForm,'nome_fisica','MariliaTeste');
theForm.submit;
end;

function TForm1.GetFormByNumber(document: IHTMLDocument2;formNumber: integer): IHTMLFormElement;
var
forms: IHTMLElementCollection;
begin
if Assigned(WebBrowser1.Document) then
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,
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;

Link para o comentário
Compartilhar em outros sites

  • 9 meses depois...
Visitante: mazinsw
 

marilia, tem uma maneira fácil e rápida de fazer isso analise o código abaixo!

{adicione o componente IdHTTP da paleta Indy Clients}

procedure TForm1.Button1Click(Sender: TObject);

var

Ts : TStringList;

CODfonte : String;

begin

Ts := TStringList.Create;

try

Ts.Add('dado1=valor1');

Ts.Add('dado2=valor2');

Ts.Add('dado3=valor3');

IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';

CODfonte := IdHTTP1.Post('http://www.site.com/arquivo.php',Ts);

memo1.Text := CODfonte;

finally

Ts.free;

end;

end;

qualquer dúvida mande um e-mail para [email protected] que postarei as duvidas nesse tópico

Link para o comentário
Compartilhar em outros sites

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Sobre o Clube do Hardware

No ar desde 1996, o Clube do Hardware é uma das maiores, mais antigas e mais respeitadas comunidades sobre tecnologia do Brasil. Leia mais

Direitos autorais

Não permitimos a cópia ou reprodução do conteúdo do nosso site, fórum, newsletters e redes sociais, mesmo citando-se a fonte. Leia mais

×
×
  • Criar novo...