2017-08-09 186 views
0

我正在使用下面的代碼來填寫一個用戶名和密碼到他們各自的網站登錄表單中的字段。TWebBrowser自動登錄

var 

Doc: IHTMLDocument2; 
I: Integer; 
Element: OleVariant; 
Elements: IHTMLElementCollection; 
Sub: Variant; 

begin 

Doc := WebBrowser1.Document as IHTMLDocument2; 
Elements := Doc.All; 
for I := 0 to Elements.length - 1 do begin 
Element := Elements.item(I, varEmpty); 

if (UpperCase(Element.tagName) = 'INPUT') and (UpperCase(Element.Type) = 'TEXT') then begin 
if (Element.name = 'user') then Element.value := 'theusername'; 

if (UpperCase(Element.tagName) = 'INPUT') and (UpperCase(Element.Type) = 'PASSWORD') then begin 
if (Element.name = 'passwrd') then Element.value := 'thepassword'; 
end; 
end; 
Sub := WebBrowser1.Document; 
Sub.frmLogin.Submit(); 
end; 
end; 

信息在各自的領域:

enter image description here enter image description here

當我運行的代碼發生了什麼事:

enter image description here

正如你所看到的,用戶名部分作品,用戶名被插入。密碼字段,但是,不。

我在做什麼錯?

+2

'大寫(Element.tagName)= 'input''和下一個看起來不正確 - 'UpperCase'贏得' T匹配'輸入' – BrakNicku

+0

@BrakNicku編輯與代碼的問題,我的一個錯字,對此感到遺憾。無論哪種方式,它不起作用,有或沒有大寫字母等,同樣的事情發生。 – Petzy

+1

只有第一個(外)'IF'被檢查循環中的每一次 – BrakNicku

回答

2

這個問題很難用格式來看。以下是該代碼的副本 - 主觀 - 更好的格式。在使用Webbrowser1進行操作之前,您可能會注意到end;。這是您的if s的關閉end; s,因此它們是嵌套的。密碼字段永遠不會被找到,因爲它不符合兩個條件。

雖然代碼格式化是一個口味問題,但有些事情確實可以幫助避免麻煩並使代碼更具可讀性。

原單格式化:

var 
    Doc: IHTMLDocument2; 
    I: Integer; 
    Element: OleVariant; 
    Elements: IHTMLElementCollection; 
    Sub: Variant; 
begin 
    Doc := WebBrowser1.Document as IHTMLDocument2; 
    Elements := Doc.All; 
    for I := 0 to Elements.length - 1 do begin 
    Element := Elements.item(I, varEmpty); 

    if (UpperCase(Element.tagName) = 'INPUT') and (UpperCase(Element.Type) = 'TEXT') then 
    begin 
     if (Element.name = 'user') then Element.value := 'theusername'; 

     if (UpperCase(Element.tagName) = 'INPUT') and (UpperCase(Element.Type) = 'PASSWORD') then 
     begin 
     if (Element.name = 'passwrd') then Element.value := 'thepassword'; 
     end; 
    end; 
    Sub := WebBrowser1.Document; 
    Sub.frmLogin.Submit(); 
    end; 
end; 

邏輯解決的問題:

var 
    Doc: IHTMLDocument2; 
    I: Integer; 
    Element: OleVariant; 
    Elements: IHTMLElementCollection; 
    Sub: Variant; 
begin 
    Doc := WebBrowser1.Document as IHTMLDocument2; 
    Elements := Doc.All; 
    for I := 0 to Elements.length - 1 do begin 
    Element := Elements.item(I, varEmpty); 

    if (UpperCase(Element.tagName) = 'INPUT') and (UpperCase(Element.Type) = 'TEXT') then 
    begin 
     if (Element.name = 'user') then 
     Element.value := 'theusername'; 
    end; 
    if (UpperCase(Element.tagName) = 'INPUT') and (UpperCase(Element.Type) = 'PASSWORD') then 
    begin 
     if (Element.name = 'passwrd') then 
     Element.value := 'thepassword'; 
    end; 
    Sub := WebBrowser1.Document; 
    Sub.frmLogin.Submit(); 
    end; 
end; 
+0

爲我提供使用互聯網代碼的權利,而不是根據自己的風格進行檢查和格式化。謝謝,工作。 – Petzy

+2

個人而言,我會使用'IHTMLDocument3.getElementById()'或'IHTMLDocument3.getElementsByName()'來查找''元素,而不是迭代'Doc.all'。或者更好的辦法是在文檔中找到'

'元素,獲取它的'IHTMLFormElement'接口,使用它的'item()'方法找到''元素,並直接調用它的submit()方法,變種「。 –

+0

@RemyLebeau感謝您的建議! – Petzy