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"*)
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 atStartWebSession
andWebExecute
in the documentation. You cannot do this exclusively within Mathematica. $\endgroup$WebExecute
work with local HTML file? $\endgroup$WebExecute["OpenPage" -> "file:///C:/local.html?param=some%20dfsf"]
$\endgroup$WebExecute
? $\endgroup$