1

I'm trying to get the MathML code using the MathJax, I've used the Selenium to automate this process but unable to get the rendered output.

`private string GetMathMLCode(string LaTexCode) {

        string webaddress = string.Empty;
        string htmltext = @"<!DOCTYPE html>" + "\n" + @"<html>" + "\n" + @"<head>" + "\n" + @"<title>MathJax TeX to MathML Page</title>" + "\n" + @"<script>" + "\n" + @"function toMathML(jax,callback) {" + "\n" + @"  var mml;" + "\n" + @"  try {" + "\n" + @"    mml = jax.root.toMathML("""");" + "\n" + @"  } catch(err) {" + "\n" + @"    if (!err.restart) {throw err} // an actual error" + "\n" + @"    return MathJax.Callback.After([toMathML,jax,callback],err.restart);" + "\n" + @"  }" + "\n" + @"  MathJax.Callback(callback)(mml);" + "\n" + @"}" + "\n" + @"</script>" + "\n" + @"<script type=""text/x-mathjax-config"">" + "\n" + @"  MathJax.Hub.Config({" + "\n" + @"    tex2jax: {inlineMath: [[""$"",""$""],[""\\("",""\\)""]]}" + "\n" + @"  });" + "\n" + @"  MathJax.Hub.Queue(" + "\n" + @"    function () {" + "\n" + @"      var jax = MathJax.Hub.getAllJax();" + "\n" + @"      for (var i = 0; i < jax.length; i++) {" + "\n" + @"        toMathML(jax[i],function (mml) {" + "\n" + @"            document.getElementById(""myText"").value = ""<?xml version=\""1.0\""?>\r\n"" + mml;" + "\n" + @"        });" + "\n" + @"        document.getElementById(""myText"").select();" + "\n" + @"        document.execCommand('copy')" + "\n" + @"      }" + "\n" + @"    }" + "\n" + @"  );" + "\n" + @"</script>" + "\n" + @"<script type=""text/javascript"" src=""http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML-full " + "\n" + @"""></script>" + "\n" + @"</head>" + "\n" + @"<body>" + "\n" + @"<div style=""text-align: center""> " + "\n" + @"    <p style=""font-weight:bold; font-size: 18pt"">Math ML Converter</p>" + "\n" + @"    <p style=""margin-top: 0.4in; text-decoration: underline; font-size: 14pt; font-variant: small-caps"">Equation</p>" + "\n" + @"    <p id=""InputText"" style=""border-style: solid; border-width: 1px""></p>" + "\n" + @"    <p style=""margin-top: 0.4in; text-decoration: underline; font-size: 14pt; font-variant: small-caps"">MathML Code</p>" + "\n" + @"    <p><textarea rows=""30"" cols=""200"" id=""myText""></textarea>   </p>" + "\n" + @"</div>" + "\n" + @"</body>" + "\n" + @"</html>";
        htmltext = htmltext.Replace(@"<p id=""InputText"" style=""border-style: solid; border-width: 1px""></p>", @"<p id=""InputText"" style=""border-style: solid; border-width: 1px"">" + LaTexCode + "</p>");
        string localpath = Path.Combine(Path.GetTempPath(), "mbdprocess.html");
        File.WriteAllText(localpath, htmltext, Encoding.UTF8);


        var Options = new ChromeOptions();
        Options.AddArgument("headless");



        IWebDriver webdriver = new ChromeDriver();

        webaddress = localpath;
        webdriver.Url = webaddress;
        webdriver.Navigate();           

        Thread.Sleep(5000);

        IWebElement element = webdriver.FindElement(By.TagName("textarea"));
        string MathMLTxt = element.GetAttribute("value");

        webdriver.Quit();           
        return MathMLTxt;
    }'

I expect to get the mathml code, but it always returns the null value.

1 Answer 1

1

Quickly skimming through your code, I'm not entirely sure what the JS would do, but I'm guessing it's throwing an error as you're using selenium syntax to locate the element inside the JS. You can use document.querySelector instead.

As a side note, you tagged this with phantomJS, but you aren't using it. Also, it helps make the code much easier to read if the lines are kept short. You can use a verbatim string literal which will allow you to make the html/JS/CSS much nicer in your code.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your answer your suggestion was very helpful, the above code works well, there was a issue in querying html tags..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.