Mathematica'da java-script kodu kullanma

Sep 04 2020

"C: \ local.html" de şu HTML dosyam var:

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

Dosyayı web tarayıcımda "file: /// C: /local.html? Param = Bazı ilginç metin" gibi açarsam, "Bazı ilginç metinler" metninin geçtiği metin alanını içeren web sayfam var URL değişkenine göre java-script param.

Şimdi aynı dosyayı Mathematica'ya aynı URL parametresiyle içe aktarmak ve ardından textarea id="path_text"java betiği çalıştırıldıktan sonra içeriğini çıkarmak istiyorum .

Aşağıdaki kod

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

"Dosya bulunamadı" hatası veriyor.

Aşağıdaki iki kod hata vermez, ancak java kodunun çalıştırılmaması veya / ve URL parametresinin geçmemesi gibi hiçbir veri de vermez.

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

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

URL parametreleri aktarılan yerel HTML dosyası, çalıştırılan java-script ve java-script'e nasıl aktarılır?

GÜNCELLEME:

Kullanmak flinty en tavsiye kullanma StartWebSessionve WebExecuteben java-komut dosyası kodu ile metin alanına içeriği ayıklamak için nasıl düşündüm:

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"*)

Yanıtlar

3 flinty Sep 04 2020 at 16:07

Sayfayı Firefox ile yükleyebilir ve öğeyi alabilirim. Ancak Wolfram'ın XPath aracılığıyla öznitelikleri ayıklamayı uyguladığını sanmıyorum. "JavascriptExecute"Ancak eserleri kullanmak .

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]