自动登录(webBrowser) [英] Automated Log In (webBrowser)

查看:181
本文介绍了自动登录(webBrowser)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试登录(执行一些例行任务)到网页(www.soccerproject.com),但是由于提交按钮类是"superbutton"而没有click()处理程序,因此我无法执行此操作,或以ID开头的ID.我尝试执行绑定到按钮的onClick方法的JavaScript,但是它没有用,所以这是我的代码,如果有人可以提供帮助,我将非常感激.

I am trying to log in (to perform some routine tasks) into a webpage (www.soccerproject.com) and i am unable to do it since the submit buttons class is "superbutton" which doesnt have the click() handler, or an ID to begin with.i tried to execute the JavaScript bound to the onClick method of the button but it didnt work, so here is my code and i will be thankful if someone could provide some help.

procedure TForm1.Button1Click(Sender: TObject);
begin
WebBrowser1.Navigate('http://www.soccerproject.com/spnewl_index.php');
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var ii:integer ;
begin
if (WebBrowser1.LocationURL='http://www.soccerproject.com/spnewl_index.php') and (i<4) then inc(i);

if i=4 then begin
  WebBrowser1.OleObject.Document.getElementById('login').setAttribute('value', Edit1.Text);
  WebBrowser1.OleObject.Document.getElementById('password').setAttribute('value', Edit2.Text);  

  wait(200);
  WebBrowser1.OleObject.Document.forms[0].submit();
  WebBrowser1.Navigate('http://www.soccerproject.com/#');
  end;
end;

我计数为4的原因是那是webBrowser需要完全加载和显示网站(以便能够填写文本)的时间.另外,wait()函数仅等待200毫秒(请确保).预先感谢

the reason i count to 4 is that thats the time the webBrowser needs to fully load and display the website (to be able to fill in the text). Also, the wait() function simply waits 200 milliseconds (just to be sure). Thanks in advance

推荐答案

您的代码中存在许多问题.计数和等待过程实际上是没有必要的.提供的代码显示了如何检测页面何时完全加载.不需要第二次调用Navigate,因为提交表单将导致浏览器加载主页. 此代码已在提供的网站上进行了测试,并且可以正常工作:)

There are a number of problems in your code. The counting and wait procedure are really not necessary. The code provided shows you how to detect when the page has completely loaded. The second call to Navigate is not needed because submitting the form will cause the browser to load the main page. This code has been tested with the provided site and works :)

unit u_frm_main;

interface

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

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    Button1: TButton;
    procedure WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
 WebBrowser1.Navigate('http://www.soccerproject.com/spnewl_index.php');
end;

procedure TForm1.WebBrowser1DocumentComplete(ASender: TObject; const pDisp: IDispatch; var URL: OleVariant);

var
  CurrentBrowser: IWebBrowser2;
  TopBrowser: IWebBrowser2;
  Document: OleVariant;
  Doc3 :  IHTMLDocument3;
  Frm  :  IHtmlFormElement;

begin
 CurrentBrowser := pDisp as IWebBrowser2;
 TopBrowser := (ASender as TWebbrowser).DefaultInterface;
 if Assigned(CurrentBrowser) and Assigned(TopBrowser) then
  begin
   if CurrentBrowser = TopBrowser then
    begin
     Doc3 := CurrentBrowser.Document as IHTMLDocument3;
     Webbrowser1.OnDocumentComplete := nil; // remove handler to avoid reentrance
     Doc3.getElementById('login').setAttribute('value', 'SO17999392', 0);
     Doc3.getElementById('password').setAttribute('value', 'XXXXX', 0);
     Frm := Doc3.getElementById('indexform') as IHtmlFormElement;
     if Assigned(Frm) then
      Frm.submit;
    end;
  end;
end;

end.

这篇关于自动登录(webBrowser)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆