Delphi Twebbrowser识别html元素id

时间:2015-03-09 14:07:43

标签: html delphi getelementbyid twebbrowser

所有,我们有一个基于网络的RFID应用程序。我们正在尝试在我们的工厂内制作显示信息的显示板。我试图让网页自动登录,但我没有运气。我已经尝试枚举页面上的所有标签/元素,但我没有运气。

使用下面的行访问违规行为(因为找不到元素j_username,但它在html片段中清楚显示。我知道我错过了一些简单的内容。有人可以帮忙吗?< / p>

 WebBrowser1.OleObject.Document.GetElementByID('j_username').
     setAttribute('value', 'dempseyw');

这是我尝试自动化的页面中的登录代码。

<html xmlns="http://www.w3.org/1999/xhtml"><head>
<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
<meta http-equiv="Pragma" content="no-cache" />
<meta http-equiv="Expires" content="0" />
        <title>MobileView</title>
        <style type="text/css">
                    .loginTextCss{
                             padding:0 0 0 22;
                    }


            #javax_faces_developmentstage_messages{
                display: none;
            }
        </style></head><body onload="document.getElementById('j_username').focus()" style="margin:0px; overflow: hidden;">
    <div style="display:none" id="notLogedInMarker"></div>
    <form action="http://dmh-rfidweb/asset-manager-web/j_spring_security_check" method="post">
        <table border="0" align="center" height="100%" width="687" cellpadding="0" cellspacing="0">
        <tr>
            <td height="50%" valign="top">
                <img src="http://dmh-rfidweb/asset-manager-web/images/spacer.gif" border="0" width="1" height="1" />
            </td>
        </tr>

        <tr>
            <td class="directionCss"><span style="font-size:12px; font-weight: bold; color: #0000FF; font-family:arial"></span>
            </td>
        </tr>


        <tr>
            <td height="427" valign="top" background="http://dmh-rfidweb/asset-manager-web/images/branding/healthcare/bg_login.jpg" style="background-repeat:no-repeat; color:#000000; font-family:arial, Helvetica, sans-serif; font-size:14px;">
                <div style="width:200px;height:150px;float:right;">
                    <table border="0" width="100%" cellpadding="0" cellspacing="0">

                    <tr>
                        <td style="padding:3 6 0 22;" valign="bottom">
                            <br clear="all" />
                            <a href="https://www.stanleyhealthcare.com/" target="new"><img src="http://dmh-rfidweb/asset-manager-web/images/branding/healthcare/logo_mobileview.gif" style="border:1; hspace:2;" />
                            </a>
                        </td>
                    </tr>
                    </table>
                </div>
                <br clear="all" />

                <div style="height:50; padding:0 0 0 15;">
                    <div style="height:10;"></div>
                </div>

                <div class="loginTextCss"><label for="j_username">User Name:</label>
                    <br /><input id="j_username" name="j_username" type="text" value="" style="font-size:16px; width:170px;" />
                    <br />
                    <br /><label for="j_password">Password:</label>
                    <br /><input type="password" id="j_password" name="j_password" value="" style="font-size:16px; width:170px;" />
                    <br />
                        <div style="font-size:11;">
                            <input id="rememberMe" type="checkbox" name="_spring_security_remember_me" title="Remember me on this computer" checked="Checked" />
                            <label for="rememberMe" title="Remember me on this computer">Remember me on this computer</label>
                        </div>

                    <div style="padding-left:112px;padding-top:20;">
                        <input type="submit" value="Log In" style="padding:3 10 3 10;color:#000000; font-family:arial, Helvetica, sans-serif;font-size:14px;" />
                    </div>
                </div>
            </td>
        </tr>

        <tr>
            <td height="50%" valign="top">
                <img src="http://dmh-rfidweb/asset-manager-web/images/spacer.gif" border="0" width="1" height="1" />
            </td>
        </tr>

        </table>
    </form></body>
</html>

1 个答案:

答案 0 :(得分:1)

您必须找到表单,然后您可以在表单中找到并设置元素的值。

使用TWebBrowser导航到网页,然后尝试以下操作。注意,这不考虑帧的可能性。

function 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 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
  else
    raise Exception.Create('HTML Field not found: ' + fieldName);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  doc: IHTMLDocument2;
  theForm: IHTMLFormElement;
begin
  doc := WebBrowser.Document as IHTMLDocument2;
  theForm := GetFormByNumber(doc, 0);
  SetFieldValue(theForm,'j_username','dempseyw');
  SetFieldValue(theForm,'j_password','pw');
  theForm.submit;
end;