Mathematica में जावा-स्क्रिप्ट कोड का उपयोग करना
मेरे पास "C: \ local.html" पर संग्रहीत निम्न 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>
यदि मैं अपने वेब ब्राउज़र में फ़ाइल को "फ़ाइल: /// C: /local.html; param = कुछ रोचक पाठ" की तरह खोलता हूं, तो मुझे उस टेक्स्ट क्षेत्र के साथ वेब पेज मिला जिसमें पाठ "कुछ दिलचस्प पाठ" शामिल है जिसे पारित किया गया था java-script URL URL द्वारा param।
अब मैं उसी URL पैरामीटर के साथ उसी फ़ाइल को Mathematica पर आयात करना चाहता हूं और फिर textarea id="path_text"java स्क्रिप्ट निष्पादित होने के बाद सामग्री को निकालता हूं ।
निम्नलिखित कोड
Import["file:///C:/local.html?param=some dfsf"]
त्रुटि "फ़ाइल नहीं मिली"।
निम्नलिखित दो कोड कोई त्रुटि नहीं देते हैं, लेकिन कोई डेटा भी नहीं देते हैं, जैसे कि जावा-स्क्रिप्ट कोड निष्पादित नहीं किया गया था या / और URL पैरामीटर पारित नहीं हुआ था।
Import["file:///C:/local.html",
Parameters -> {"param" -> "Some interesting text"}]
Import["C:\\local.html",
Parameters -> {"param" -> "Some interesting text"}]
जावा-स्क्रिप्ट और जावा-स्क्रिप्ट निष्पादित URL मापदंडों के साथ स्थानीय HTML फ़ाइल कैसे आयात करें?
अपडेट करें:
उपयोग करने के लिए फ्लिन्टी की सलाह का उपयोग करना StartWebSessionऔर WebExecuteमुझे पता चला कि जावा-स्क्रिप्ट कोड द्वारा पाठ क्षेत्र से सामग्री कैसे निकाली जाए:
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"*)
जवाब
मैं पृष्ठ को फ़ायरफ़ॉक्स के साथ लोड कर सकता हूं , और तत्व प्राप्त कर सकता हूं । लेकिन मुझे नहीं लगता कि वुल्फराम ने एक्सपीथ के माध्यम से एक्सट्रैक्टिंग विशेषताओं को लागू किया है। "JavascriptExecute"हालांकि कार्यों का उपयोग करना ।
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]