Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read javascript select options into Delphi 1

Status
Not open for further replies.

FranzM01

Technical User
Jan 27, 2008
3
NL
I receive a web page which has javascript in it.

Code:
<select name="region52708" class="popup" onchange="javascript:selectRegion(52708)">
<option value=""></option>
<option selected value="N America">N America</option>
<option value="Asia">Asia</option>
<option value="Europe">Europe</option>
<option value="Global">Global</option>
<option value="S America">S America</option>
<option value="Africa">Africa</option>
</select>
I want to copy the select option values, i.e. Asia, Europe, etc from the select statement in javascript. I am using IE as browser, Delphi 7 and TWebBrowser.

ovElements.item(i).name points at the select belonging to region52708

I try this:

for j := 0 to ovElements.item(i).options.length-1 do begin
Memo1.lines.add('option ' + inttostr(j) + ' ' + ovElements.item(i).options[j].value;
end;

The syntax is not correct.
Is someone able to point me at the correct way to make the options from the script visible in Delphi?

Thanks in advance.

Regards,

Franz
 
You seem to be missing a closing parenthesis:
Code:
for j := 0 to ovElements.item(i).options.length-1 do begin
Memo1.lines.add('option ' + inttostr(j) + ' ' + ovElements.item(i).options[j].value[b][COLOR=red text])[/color][/b];
end;
It usually helps to provide us with the actual compiler error message.

Andrew
Hampshire, UK
 
Right, ")" was missing in text, not in the code itself.
Error message is:
Project xx.exe raised exception class EOleError with message 'Method 'value' not supported by automation object'. Process stopped. Use Step or Run to continue.

Question is: how to access the array of options?
 
You need to be more precise. This appears to be a run-time error rather than a syntax error. You also need to tell us what type or class is ovElements - how it was created and so on.

However, the error message seems to be pretty clear. Have you checked what properties and/or methods are provided by the automation object (presumably ovElements)?

Andrew
Hampshire, UK
 
Error message is a run time error. I expect to be caused by a syntax error in the Memo1 statement: how to address the array of options in a javascript select statement?

First part of code:

WebBrowser1.Navigate('
// Get reference to current document
Doc := WebBrowser1.Document as IHTMLDocument2;
if not Assigned(Doc) then Exit;


then call FillInForm:

FillInForm(WebBrowser1, Doc, WebBrowser1.Document, 'Global');

procedure TForm1.FillInForm(WB: ShDocVW.TWebbrowser; IDoc1: IHTMLDocument2; Document: Variant; OptionSelectedName: string);
var
IEFields: array[1..2] of string;
var
IEFieldsCounter: Integer;
i,j,m: Integer;
ovElements: OleVariant;
begin
IEFields[1] := 'SELECT';
IEFields[2] := 'region' + session_id;

if Pos('Form1', Document.Title) <> 0 then

while WB.ReadyState <> READYSTATE_COMPLETE do
Application.ProcessMessages;

// count forms on document and iterate through its forms
IEFieldsCounter := 0;
for m := 0 to Document.forms.Length - 1 do
begin
ovElements := Document.forms.Item(m).elements;

memo1.lines.add('ovElements.Length: ' + IntToStr(ovElements.Length));
// iterate through elements
for i := ovElements.Length - 1 downto 0 do
begin
try
if ovElements.item(i).tagName <> 'INPUT' then
// if input field found, try to fill it out
if (ovElements.item(i).tagName = IEFields[1]) and
(ovElements.item(i).name = IEFields[2]) then
begin
ovElements.item(i).Value := OptionSelectedName;
Inc(IEFieldsCounter);
Memo1.lines.add('option length ' + inttostr(ovElements.item(i).options.length));
//try to list all possible options
for j := 0 to ovElements.item(i).options.length-1 do begin
Memo1.lines.add('option ' + inttostr(j) + ' ' + ovElements.item(i).options[j].value);
end;
end;

except
// failed...
end;
end; { for i...}
end; { for m }
// if the fields are filled in, submit.
// etcetra
// ....
end;
 
Try:
Code:
Memo1.lines.add('option  ' + inttostr(j) + ' ' + ovElements.item(i).options[j].text);

I believe what you had originally was correct, according to the DOM but, as Andrew mentioned, I'm not sure how the automation object represents it. You might have to do some digging in MSDN.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
I've found some further reading I think you'll find helpful:

Hope this points you in the right direction.

Clive
Runner_1Revised.gif

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"To err is human, but to really foul things up you need a computer." (Paul Ehrlich)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
To get the best answers from this forum see: faq102-5096
 
Clive, those look like very useful links. Have a star!

Andrew
Hampshire, UK
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top