3
$\begingroup$

I'm downloading a file by clicking a button from chrome with the WebDriver functionality:

sess = StartWebSession[Visible->True]
WebExecute[sess, "ClickElement" -> el] (* el is download button *)

This works just fine, and when it finishes it gives a success object:

Success["ClickElement",  Association[
 "MessageTemplate" :> "`command` was successful.", 
  "MessageParameters" -> Association["command" -> "ClickElement"], 
  "Element" -> "0.5797105527746262-24"]]

My question is two-fold:

  1. I need a file handle to the finished download (and if it failed to download I need to know that).
  2. Why doesn't it work when session is not visible. In particular, it gives success but doesn't actually download, for example:
sess = StartWebSession[Visible->False];
WebExecute[sess, 
 "OpenPage" -> "https://datarepository.wolframcloud.com/resources/Patient-Medical-Data-for-Novel-Coronavirus-COVID-19"];
a = WebExecute[sess, "LocateElements" -> "Tag" -> "a"];
p = Position[WebExecute[sess, "ElementText" -> a], 
   "Source Notebook"][[1, 1]];
WebExecute[sess, "ClickElement" -> a[[p]]]

enter image description here

$\endgroup$
5
  • $\begingroup$ Why don't you use Import straight on the file you want to download? $\endgroup$ Commented Nov 27, 2020 at 18:16
  • $\begingroup$ Import doesn’t work with JavaScript $\endgroup$
    – M.R.
    Commented Nov 28, 2020 at 1:34
  • $\begingroup$ But you are running it from Mathematica so what difference it would have if you use another Mathematica's function instead of WebExecute? $\endgroup$ Commented Nov 28, 2020 at 21:35
  • $\begingroup$ Import is highly restrictive with URL, it doesn’t work on anything async rendered. $\endgroup$
    – M.R.
    Commented Nov 29, 2020 at 2:40
  • $\begingroup$ ??? The Import worked pretty well for me with the file in your example "Patient-Medical-Data-for-Novel-Coronavirus-COVID-19-definition.nb". What problems do you have with it? You have also many different functions for downloading like URLDownload or URLDownloadSubmit and others. Give me an example when these functions and Import do not work. $\endgroup$ Commented Nov 29, 2020 at 10:57

1 Answer 1

1
+50
$\begingroup$

Regarding that your code does not work with StartWebSession[Visible->False] seems to be a bug.

Here is my code that works with StartWebSession[Visible->False] but uses URLDownloadSubmit instead of WebExecute. If everything goes OK and the file is downloaded you get no message, if the file does not exist or something goes wrong you get a warning message. You can specify your own name of downloaded file and its path instead of "C:\\file.nb". So this answer both of your questions.

sess = StartWebSession[Visible -> False];
WebExecute[sess, 
  "OpenPage" -> 
   "https://datarepository.wolframcloud.com/resources/Patient-Medical-\
Data-for-Novel-Coronavirus-COVID-19"];
a = WebExecute[sess, "LocateElements" -> "Tag" -> "a"];
p = Position[WebExecute[sess, "ElementText" -> a], 
    "Source Notebook"][[1, 1]];
URLDownloadSubmit[
 WebUnit`Private`attribute[
   sess /@ {"SessionID", "Browser", "URL"}, a[[p]]["ElementId"], 
   "href"], "C:\\file.nb", 
 HandlerFunctions -> <|"ConnectionFailed" -> MessageDialog|>, 
 HandlerFunctionsKeys -> "StatusCode"]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.