TransWikia.com

Get file downloaded from click in WebSession?

Mathematica Asked on July 8, 2021

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

One Answer

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"]

Correct answer by azerbajdzan on July 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP