[SOLVED] Help for downloand file lazarus / 38-Networking and Web Programming ---------------------------------------------------------------------- magleft · 2023-05-04 06:17 Hello. I want to downloand file from following url https://1click.minagric.gr/oneClickUI/frmFytoPro.zul?lang=en I want to downloand file with Lazarus  with synapse but can' t find url of file. Can someone help me? Thanks in advance. ---------------------------------------------------------------------- bobby100 · 2023-05-04 06:29 The download link is in an invisible iframe, and the iframe opens by the JavaScript after you click on "here". Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i I do not know if the link is persistent, or it is generated every time you click on "here". ---------------------------------------------------------------------- TRon · 2023-05-04 06:39 Seems like the link is not persistent ---------------------------------------------------------------------- magleft · 2023-05-04 06:41 Thanks for reply. Every time who pres button start downloand a file. If pres right button on downloand file i copy a link of file but is different every time. For example following links. https://1click.minagric.gr/oneClickUI/zkau/view/z_i2e/dwnmed- 0/g3k1/dbSYSPEST.zip https://1click.minagric.gr/oneClickUI/zkau/view/z_i2e/dwnmed- 1/tnp/dbSYSPEST.zip I think that means generete link every time when press "here" button. So how could the download of the file be done automatically through lazarus? ---------------------------------------------------------------------- BobDog · 2023-05-04 11:03 I can get a file via windows bitsadmin, but nothing to do with Lazarus, only freepascal. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    usessysutils;  function  shell(s:pchar):integer ; cdecl external 'msvcrt.dll' name 'system'; function filelength(filename:ansistring):longword;Var F : File of byte;var L:longword;beginAssign (F,filename);  Reset (F);  L:=FileSize(F);  Close (F);  exit(L);end;  procedure loadfile(var content: ansistring;filename:ansistring);   Var Fin : File;   Var x:int32;   begin   x:=filelength(filename);   setlength(content,x);   Assign (Fin,filename);   Reset (Fin,x);   BlockRead (Fin,content[1],1);   close(fin);end;  procedure download(site:ansiString;newfile:ansistrin g);varf,content:ansistring;begincontent:='';if fileexists(newfile) then deletefile(newfile);  If (pos(' ',site) <>0) Then site:=Chr(34)+site+Chr(34);  f:='type nul > ' +GetCurrentDir +'\'+newfile; // create empty file  shell (pchar(f));  shell('bitsadmin /reset');  shell ('bitsadmin /create mydownload');  f:='bitsadmin  /transfer mydownload  /download  /priority normal /DYNAMIC ' +  site +' '+ Chr(34) + GetCurrentDir +'\'+ newfile +Chr(34);  Shell(pchar(f));   shell ('bitsadmin /complete mydownload');  if (filelength(newfile)<>0) then loadfile(content,newfile) else exit;  f:=Chr(34) +GetCurrentDir +  '\'+newfile + Chr(34);  If (Length(content)>4) Then     Shell (pchar( f));End;  varsite:ansistring='https://1click.minagric.gr/oneClickUI/fr mFytoPro.zul?lang=en'; beginshell('Title Please wait a few seconds until Job completed');download(site,'New.file');writeln('Press return to finish . . .');readln;end. ---------------------------------------------------------------------- magleft · 2023-05-05 04:30 BobDog thanks for Reply. But i can't downloand file 'Syspest.zip' when downloand freepascal and compile your code. I change your code in line 59 as download(site,'SYSPEST.ZIP'); besides, I would be interested in the process being done through the Lazarus application. Anyway, I would like to thank you for your help and time ---------------------------------------------------------------------- BobDog · 2023-05-05 10:18 Sorry it doesn't work magleft. The site containing SYSPEST.ZIP always says: "The requested resource is no longer available, and no forwarding address is known." So that is the reason I reckon. I also tested through pascal code using powershell -- with the same result. To run the code in Lazarus I would use file, new, simple program,  get rid of the form and code windows if they appear. Paste the code to the source editor , get rid of the top few lines : program Project1; begin end. Then quick compile and then run without de-bugging. (But I don't use Lazarus, maybe only to test out code sometimes). ---------------------------------------------------------------------- rvk · 2023-05-05 10:40 Quote from: magleft on May 05, 2023, 06:30:39 amI change your code in line 59 as download(site,'SYSPEST.ZIP'); As already stated... direct download isn't possible. You would need to execute the javascript on that site to get a correct unique temporary downloadlink. The only option I see is simulating the click on "Here" in a browser (or browsercomponent) which can interpret javascript. ---------------------------------------------------------------------- loaded · 2023-05-05 10:40 Download link ; Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i https://1cl ick.minagric.gr/oneClickUI/zkau/view/z_m4a/dwnmed-0/d8t1/dbSYSPEST.zip ---------------------------------------------------------------------- rvk · 2023-05-05 10:42 Quote from: loaded on May 05, 2023, 12:40:50 pmDownload link ; Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i https://1cl ick.minagric.gr/oneClickUI/zkau/view/z_m4a/dwnmed- 0/d8t1/dbSYSPEST.zipFrom the "z_m4a/dwnmed-0" in the link you can see that this is a temporary link. It will expire within a short period of time. (read the previous answers) ---------------------------------------------------------------------- TRon · 2023-05-05 10:45 @loaded: as rvk stated. The javascript generates a temporary link. Just clear the cache of your browser and download the file again to see for yourself that the link is different this time. A simple use of wget using that same URL that you posted will fail as well. fwiw: the "z_m4a/dwnmed-0" part can be obtained from the html but the "d8t1" part is generated by javascript after pressing the button. ---------------------------------------------------------------------- loaded · 2023-05-05 11:26 Quote from: rvk on May 05, 2023, 12:42:37 pmFrom the "z_m4a/dwnmed-0" in the link you can see that this is a temporary link. Quote from: TRon on May 05, 2023, 12:45:05 pmThe javascript generates a temporary link. Just clear the cache of your browser and download the file again to see for yourself that the link is different this time. A simple use of wget using that same URL that you posted will fail as well. Yes you are right, I didn't know that. ---------------------------------------------------------------------- magleft · 2023-05-31 05:28 After querying the page administrator, the following response was given: Checked the link and added a constant btnid="bdf" attribute that you can use. How can it be used so that the file can be downloaded through the synapse ---------------------------------------------------------------------- rvk · 2023-05-31 07:29 Quote from: magleft on May 31, 2023, 07:28:09 amAfter querying the page administrator, the following response was given: Checked the link and added a constant btnid="bdf" attribute that you can use. How can it be used so that the file can be downloaded through the synapse You can't. You should ask how you could use that attribute to retrieve the file. There is an attribute btnid added but you don't have javascript to execute it. So you still don't know what the underlying filename would be. (The administrator probably thought you wanted to automate the DOM- object and simulate a click on that element. But you can't simulate anything without a javascript interpreter.) ---------------------------------------------------------------------- Jurassic Pork · 2023-06-01 06:33 Hello, here is a code to download the file and display the url of this file using WebDriver4L v0.2 (windows + automating chrome) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i uses  Webdriver4L;procedure TForm1.BtMinagridClick(Sender: TObject);var Robot : TWebDriver;    hereLnk,dlsrc : TWebElement;BeginTry   Robot := TChromeDriver.Create(nil);   Robot.StartDriver(ExtractFileDir(Paramstr(0)) + '\chromedriver.exe',    '--user-data-dir d:\temp\Web4L');  Sleep(2000);  Robot.NewSession;  Robot.Implicitly_Wait(2000);  Robot.Set_Window_Size(1280, 1024);  Robo t.GetURL('https://1click.minagric.gr/oneClickUI/frmFytoPro.zul?lang=en ');  hereLnk := Robot.FindElementByXPath('//span[@*[local-name() = "ns:btnid"][. ="bdf"]]');  hereLnk.click();  dlsrc := Robot.FindElementByID('zk_download');  Memo1.Append(dlsrc.AttributeValue('src'));  Sleep(30000);  Robot.Quit;  finally  Robot.Free;  end;end; Friendly J.P ---------------------------------------------------------------------- magleft · 2023-06-02 18:30 It works!!! Thanks so much for everyone's help. ---------------------------------------------------------------------- magleft · 2023-06-03 09:32 Jurassic Pork One additional question. How could we set the folder and file name to be saved? Αnd again I would like to thank you for your help. ---------------------------------------------------------------------- Jurassic Pork · 2023-06-04 05:42 Hello, the file is downloaded in the download folder of the used webbrowser. Then you can move the file and rename it in another folder with the renameFile function. Example : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  dat_old, dat_new: string;  begin  dat_old := 'C:\olddir\test.txt';  dat_new := 'C:\newdir\test.txt';   if RenameFile(dat_old, dat_new) then begin    ShowMessage('Renaming was successful!');  end else begin    ShowMessage('Error!');  end;end;Friendly, J.P ---------------------------------------------------------------------- rvk · 2023-06-04 10:24 Moving the file from downloads to the desired location feels like the easy way out (which isn't to say that's a bad thing  ) It should be possible to set the download location for the chromedriver via the ChromeOptions. https://sites.google.com/a/chromium.org/chromedriver/capabilities (Although I'm nog sure how this would be implemented in Webdriver4L.) ---------------------------------------------------------------------- magleft · 2023-06-08 15:02 Thanks !!!