C# 如何在Selenium(PhantomJs)中获得Javascript呈现的Html?

C# 如何在Selenium(PhantomJs)中获得Javascript呈现的Html?,c#,selenium,phantomjs,C#,Selenium,Phantomjs,我正在尝试使用MathJax获取MathML代码,我已经使用Selenium自动化了这个过程,但无法获得渲染输出 `私有字符串GetMathMLCode(字符串LaTexCode) { string webaddress = string.Empty; string htmltext = @"<!DOCTYPE html>" + "\n" + @"<html>" + "\n" + @"<head>" + "\n" + @"<

我正在尝试使用MathJax获取MathML代码,我已经使用Selenium自动化了这个过程,但无法获得渲染输出

`私有字符串GetMathMLCode(字符串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;
    }'
string webaddress=string.Empty;
字符串htmltext=@“+”\n“+@”+“\n”+@“+”\n“+@”MathJax TeX到MathML页面“+”\n“+”+”\n“+@”函数到MathML(jax,回调){“+”\n“+@”var mml;“+”\n“+@”尝试{“+”\n“+@”mml=jax.root.tomathl@返回MathJax.Callback.After([toMathML,jax,Callback],err.restart);“+”\n“+@”}”+“\n”+@”MathJax.Callback(Callback)(mml);“+”\n“+”}”+“\n”+“+”+”\n“+”+”\n“+”MathJax.Hub.Config({“+”\n”+@”tex2jax:{inlineMath:[“$”,“,”,“+”,[\(,“,”)”+”;“+”;“+”;“\n”+”;“+”;\njax+”+“+”+”;\nhub+”;“+”;\njax+”+“+”;\njax+”+”;\n函数(){“+”\n“+@”var jax=MathJax.Hub.getAllJax();“+”\n“+@”for(var i=0;iMath-ML转换器页边距顶部:0.4in;文本装饰:下划线;字体大小:14pt;字体变体:小大写“>公式“+”\n“+@”

“+”\n“+@”

MathML代码”

“+”\n“+”

“+”\n“+@”“+”\n“+@”“+”\n“+@”“; htmltext=htmltext.Replace(@“

”,@“

”+LaTexCode+“

”; 字符串localpath=Path.Combine(Path.GetTempPath(),“mbdprocess.html”); writealText(localpath,htmltext,Encoding.UTF8); var Options=新的ChromeOptions(); 选项。添加参数(“无头”); IWebDriver webdriver=新的ChromeDriver(); webaddress=localpath; webdriver.Url=webaddress; webdriver.Navigate(); 睡眠(5000); IWebElement元素=webdriver.FindElement(按.TagName(“textarea”); 字符串MathMLTxt=element.GetAttribute(“值”); webdriver.Quit(); 返回MathMLTxt; }'

我希望得到mathml代码,但它总是返回空值。

快速浏览您的代码,我不完全确定JS会做什么,但我猜当您使用selenium语法定位JS中的元素时,它会抛出一个错误。您可以改为使用
document.querySelector


作为补充说明,您用phantomJS标记了它,但您没有使用它。此外,如果行数保持较短,它有助于使代码更易于阅读。您可以使用逐字字符串文字,这将使您的代码中的html/JS/CSS更加出色。

感谢您的回答您的建议非常有用,上面的代码运行良好,查询html标记时出现问题。。