finding which form the input field belongs to delp

2019-08-20 23:04发布

Follow my previous question which i managed to answer myself. I am able to place text in an input field on a website served on webbrowser in a delphi application. sometimes there are multiple forms on a website, most of the time its around 2, but it could head up to a faire few. Now my question is. How can I 'test' or find out which form the selected input field belongs. Lets just take an example. For example, gmail.com there are 2 input fields: 'Email' and 'Passwd' I also know that there are 2 forms on this site. with code like this one you can select either one of them :

doc:=webbrowser1.Document as IHTMLDocument2;
frm:=doc.forms.item(0,EmptyParam) as IHTMLFormElement;
fld:=frm.item('Email',EmptyParam) as IHTMLInputTextElement;
fld.value:=GetFieldValue(theForm,'test@gmail.com');

by changing the 0 to a 1 in the second line you have the second form on the site. Now I want to know how can I let the program find out for itself which form the input field belongs too. could you do this with try? or anything else? Any have any idea?

1条回答
Viruses.
2楼-- · 2019-08-20 23:27

fld.form points to the form the element belongs to. You can use doc.getElementFromId or any other method you like to get the element without using the form to get it, although it is easiest if it has an id.

Other option is to search through all forms to check if it has a field with the given name, but that won't help you if a field exists in more than one form.

查看更多
登录 后发表回答