看样子没办法送分了,看看是否有人能替我解决一个类似的问题:
http://www.aatravel.co.nz/main/search.jsp?typeId=3
我要逐一选择页面上的Region和Type,提交网页,然后分析返回的页面内容。但我用了几种选择option的办法都不行,页面总是返回没有选择的搜索内容。
我用的程序如下:
procedure SelectOptionItem(WebBrowser: TWebBrowser; OptionName: string; ItemValue: string);
var
Doc: IHTMLDocument2;
iELC: IHTMLElementCollection ;
HtmlSelEle: IHTMLSelectElement;
iOpt: IHTMLOptionElement;
spDisp: IDispatch;
i, j: integer;
s2, s3:string;
begin
WebBrowser.Document.QueryInterface(IHTMLDocument2,Doc);
if Doc <> nil then
begin
iELC := Doc.Get_all;
for j := 0 to iELC.length - 1 do
begin
Application.ProcessMessages;
spDisp := iELC.item(J, 0);
if SUCCEEDED(spDisp.QueryInterface(IHTMLSelectElement ,HtmlSelEle))then
begin
s2 := (HtmlSelEle.name);
if AnsiSameText(S2, OptionName) then
begin
for i := 0 to HtmlSelEle.length - 1 do
begin
Application.ProcessMessages;
spDisp := HtmlSelEle.item(i, 0);
if SUCCEEDED(spDisp.QueryInterface(IHTMLOptionElement, iOpt))then
begin
s3 := iOpt.value;
if AnsiSameStr((ItemValue),(s3)) then
begin
iOpt.selected := true;
HtmlSelEle.selectedIndex := iOpt.index;
Break;
end;
end;
end;
end; // if
end; // if
end; //END FOR
end;
end;
procedure ClickSubmitButton(WebBrowser: TWebBrowser; ButtonName: string);
var
ovElements: OleVariant;
i, j: Integer;
begin
for j := 0 to (WebBrowser.OleObject.Document.forms.Length - 1) do
begin
ovElements := WebBrowser.OleObject.Document.forms.item(j).elements;
for i := 0 to (ovElements.Length - 1) do
if (UpperCase(ovElements.item(i).tagName) = 'INPUT') and
(UpperCase(ovElements.item(i).type) = 'SUBMIT') and
(ovElements.item(i).Name = ButtonName) then
begin
ovElements.item(i).Click;
Exit;
end;
end;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
WebBrowser1.Navigate('http://www.aatravel.co.nz/main/search.jsp?typeId=3');
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SelectOptionItem(WebBrowser1, 'locationId', '20029'); //Alpine Pacific Triangle Touring Route
SelectOptionItem(WebBrowser1, 'subTypeId', '1003'); //Activity with Accommodation
ClickSubmitButton(WebBrowser1, 'submit');
end;
按照我选定的搜索条件,如果直接用IE浏览器打开这个页面搜索,应该返回2条记录。但是实际在程序运行中,搜索返回了580条记录。这和没有选择任何Region和Type时得到的结果一样。看起来我用的SelectOptionItem函数有问题?但是这个函数用在其他网站的页面上又没有问题。 而且这个页面还不能用以下语句提交:
WebBrowser1.OleObject.document.forms.item(1).submit();
但愿有人能替我解决这个问题,谢谢!