tags:

views:

62

answers:

2

Hi , i'm using this code to fill out fields of some WebPages , using WebBrowser control :

procedure TFrmMain.SetValue(const document: IHTMLDocument2;

  const formNumber: Integer;

  const fieldName, newValue: string);
var
  form: IHTMLFormElement;
  field: IHTMLElement;
begin
  form := WebFormGet(formNumber, document);
  field := form.item(fieldName, '') as IHTMLElement;
  if field = nil then
    exit;
  if field.tagName = 'INPUT' then (field as IHTMLInputElement)
    .value := newValue
  else if field.tagName = 'SELECT' then (field as IHTMLSelectElement)
    .value := newValue
  else if field.tagName = 'TEXTAREA' then (field as IHTMLTextAreaElement)
    .value := newValue;
end;

But "SetValue" method can not fill out fields for this WebPage !!! : http://commenting.iranblog.com/services/commenting/commenting.aspx?BlogID=67436&PostID=754352

  SetValue(W.document as IHTMLDocument2, 0, 'name', 'ThisIsANewValueForField');

In fact, i think this code can not find the field name for this WebPage but i don't know the reason !

+1  A: 

The only field with the name "name" is the one that is defined as

<input onKeyPress=FKeyPress() id=name onBlur=hideLanguageButton() 
      onFocus=showLanguageButton(this) maxlength=50 size=50 
      name=name>

But this is after the closing form tag (</form>) of the only form on the page (form _ctl0), so is technically not in the first form. So your IHTMLFormElement that is loaded with the first form on the page will not contain the "name" field.

Re0sless
+1  A: 

If WebFormGet returns nil, then you call a method on that variable, which will surely crash your program. You didn't say your code crashes, so we can assume that WebFormGet successfully acquires the form.

But if WebFormGet doesn't return nil, then you never assign any value at all to field, so it will have its compiler-assigned default value of nil. You check whether field = nil and exit.

So, right away, we can conclude that the problem you're reporting right now has absolutely nothing to do with the particular Web page you're testing it on. Your code will fail on all pages. It's not that your code cannot find the field; your code never even looks for the field.

You could have discovered this problem with your debugger. Don't be afraid to use the tools you have. You could set a breakpoint in that procedure and step through the code one line at a time. At each line, confirm that what you expect to be true really is true. You were expecting field to refer to the field you named, but instead it's always nil. You could then look back through the preceding code to discover the cause. You would look at all the possible ways that field could attain a non-null value, and then figure out why none of those things happened. You would eventually re-read your code carefully enough to discover that there is no way for field to attain a value.

If you fix that problem and your code still doesn't work, repeat the same process. Use the debugger to step through your code and make sure each step works as you expected. If it doesn't work, try to determine what it is that failed.

Rob Kennedy
Thank you Mr Kennedy ! As Re0sless said , Field ('name') is not in the form(0) and when i trace my code (with Shift+F7) i realized that result for "WebFormGet" is not nil . thus , we have the form but our fields are not in this form . so how can i find the fields when they're not in any form ?
Kermia
Ignore the form and use [`getElementById`](http://msdn.microsoft.com/en-us/library/aa752543.aspx) or [`getElementsByName`](http://msdn.microsoft.com/en-us/library/aa752544.aspx) instead.
Rob Kennedy
i edited my code ! It had a problem
Kermia
I know it did. I told you that.
Rob Kennedy