2017-08-09 106 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