2
$\begingroup$

I have the following HTML file stored on "C:\local.html":

<html>
<head>
<script type="text/javascript">

function execute()
{
    const urlParams = new URLSearchParams(window.location.search);
    const param = urlParams.get('param');
    var text_box = document.getElementById("path_text");
    text_box.value = param;
}

</script>

</head>
<body onload="execute()">

<textarea id="path_text" rows="40" cols="80" style="font-size:75%"></textarea><br/>

</body>
</html>

If I open the file in my web browser like "file:///C:/local.html?param=Some interesting text" I got web page with the text area that contains the text "Some interesting text" that was passed to the java-script by URL variable param.

Now I want to import the same file to Mathematica with the same URL parameter and then extract the content of textarea id="path_text" after the java script was executed.

The following code

Import["file:///C:/local.html?param=some dfsf"]

gives error "File was not found".

The following two codes gives no error but also gives no data, like the java-script code was not executed or/and the URL parameter was not passed.

Import["file:///C:/local.html", 
 Parameters -> {"param" -> "Some interesting text"}]

Import["C:\\local.html", 
 Parameters -> {"param" -> "Some interesting text"}]

How to import local HTML file with passed URL parameters to java-script and java-script executed?

UPDATE:

Using flinty's advise to use StartWebSession and WebExecute I figured out how to extract the content from text area by java-script code:

session = StartWebSession[Visible -> False];
WebExecute[
  "OpenPage" -> "file:///C:/local.html?param=Some interesting text"];
WebExecute[session, 
 "JavascriptExecute" -> 
  "return document.getElementById('path_text').value"]
DeleteObject[session]
Clear[session]

(*"Some interesting text"*)
$\endgroup$
  • 2
    $\begingroup$ URLRead["file:///C:/local.html?param=some dfsf"] will download the html. But the JS will not execute. Mathematica is not a web browser. Have a look at StartWebSession and WebExecute in the documentation. You cannot do this exclusively within Mathematica. $\endgroup$ – flinty Sep 4 at 14:40
  • $\begingroup$ Can WebExecute work with local HTML file? $\endgroup$ – azerbajdzan Sep 4 at 15:15
  • $\begingroup$ Yes. WebExecute["OpenPage" -> "file:///C:/local.html?param=some%20dfsf"] $\endgroup$ – flinty Sep 4 at 15:18
  • $\begingroup$ OK and then how I extract the content of the text area when the page is open with WebExecute? $\endgroup$ – azerbajdzan Sep 4 at 15:21
  • $\begingroup$ WebExecute works fine for me. It should be opening firefox or chrome if installed. $\endgroup$ – flinty Sep 4 at 15:59
3
$\begingroup$

I can load the page with Firefox, and get the element. But I don't think Wolfram have implemented extracting attributes via XPath. Using "JavascriptExecute" works however.

session = StartWebSession[Visible -> False];
WebExecute[session, {
  "OpenPage" -> "file:///C:/local.html?param=some%20dfsf"
}]

(* here we try to get the element and it works *)
e = WebExecute[session, {
  "LocateElements" -> "XPath" -> "//*[@id='path_text']"
}]

(* here we try to get the .value attribute, but it won't work and returns {{}}.
 This XPath query doesn't even work in the Firefox console either. *)
a = WebExecute[session, {
  "LocateElements" -> "XPath" -> "//*[@id='path_text']/@value"
}]

(* we can try to execute js on the page to extract the attribute, this works *)
result = WebExecute[session, 
  "JavascriptExecute" -> "return document.getElementById('path_text').value"]

DeleteObject[session]
| improve this answer | |
$\endgroup$
  • $\begingroup$ Anyway it is a very cumbersome way of doing it but now we can use any java-script code, pass some parameters to it from Mathematica, execute java-script and then get the results back to Mathematica. Still less cumbersome than installing NodeJS and all its dependencies to just find out it will not work anyway. $\endgroup$ – azerbajdzan Sep 4 at 16:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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