program NewbieScript; { More complex script that prompts user for book search keyword in Amazon. The first 10 pages of search results are save in an Excel file by clicking the 'More Results' image button. More difficult since image button does not have a name, so we using Tab and Enter keys to access the button } const ExcelFile = 'c:\amazon.xls'; MaxPages = 10; var sData : string; sSearchStr : string; nPageCount : integer; bFirstPage : boolean; { Process events here } procedure OnDocumentComplete(URL : string); begin if IsPartOf('http://www.amazon.com/exec/obidos/ats-query-page', URL) then begin // assign search string to title field Fill('field-title', sSearchStr); // search button does not have a name, no way to reference it, just press enter SendKeyPress('Enter'); end; // make sure not greater than 10 pages if (IsPartOf('http://www.amazon.com/exec/obidos/search-handle-form', URL) or IsPartOf('http://www.amazon.com/exec/obidos/search-handle-url', URL)) and (nPageCount <= MaxPages) then begin // get all results at html table 23,1,2; use ClickCapture to get position sData := GetTableCell(23,1,2); // append extracted results to file TextAppendToFile(ExcelFile, sData); // increment counter nPageCount := nPageCount + 1; // no direct way to access 'More Results' image button // improvise by going to email address field and tab over 2 (first page only) // or 3 times and press return (same as clicking the image button) Fill('email_address', ''); SendKeyPress('TAB'); SendKeyPress('TAB'); // first page does not need extra tab if not(bFirstPage) then SendKeyPress('TAB'); SendKeyPress('ENTER'); bFirstPage := False; end // stop if retrieved max pages else if (nPageCount > MaxPages) then NewbieScriptEnd; end; { This is the main program body. } begin nPageCount := 1; bFirstPage := True; DeleteFile(ExcelFile); // ask what title to search for sSearchStr := Readln('Enter search string:'); Navigate('http://www.amazon.com/exec/obidos/ats-query-page/ref=b_tn_bh_bo/002-3070511-5218419'); end.