简体   繁体   中英

Delphi Twebbrowser Identify html element id

All, we have an RFID application that is web based. We are trying to make display boards across our facility that shows information. I'm trying to make the webpage auto-login but I'm not having any luck. I've tried enumerating all tags/elements on the page but I'm not having any luck.

Access violation with the line below (because the element j_username isn't found, but it's clearly in the html snippet. I know I'm missing something simple. Can someone please help?

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

here is the login code from the page I am trying to automate.

<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>

You have to locate the form, then you can locate and set the value of the element within the form.

Navigate to the webpage with TWebBrowser, then try the following. Note, this does not consider the possibility of frames.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM