简体   繁体   English

如何使用casperjs从嵌入式脚本中检索文本?

[英]How do I retrieve text from an embedded script using casperjs?

the html element is html元素是

<script>
window.sawXmlIslandidClientStateXml="<nqw xmlns:saw=\x22com.siebel.analytics.web/report/v1.1\x22 xmlns:xsi=\x22http://www.w3.org/2001/XMLSchema-instance\x22 xmlns:sawst=\x22com.siebel.analytics.web/state/v1\x22>\u003csawst:clientState>\u003csawst:stateRef>\u003csawst:envState xmlns:sawst=\"com.siebel.analytics.web/state/v1\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlVersion=\"200811100\">\u003csawst:container cid=\"d:dashboard\" xsi:type=\"sawst:topLevelStateContainer\">\u003csawst:container cid=\"p:mco0pb0nob7sqjvg\" xsi:type=\"sawst:page\">\u003csawst:container cid=\"s:42263r43nih80fd1\" xsi:type=\"sawst:section\" rendered=\"true\">\u003csawst:container cid=\"g:c452lvndqssjqa45\" xsi:type=\"sawst:dashprompt\" links=\"-\" promptAutoCompleteState=\"off\"/>\u003c/sawst:container>\u003csawst:container cid=\"r:q4g2fiisnvk4nusv\" xsi:type=\"sawst:report\" links=\"fd\" defaultView=\"compoundView!1\" searchId=\"fvup02s9lt0o6urkplv4pqa5ri\" folder=\"/shared/Sales\" itemName=\"All Sales and Inventory Data\"/>\u003csawst:container cid=\"f:dpstate\" xsi:type=\"sawst:dashpromptstate\" statepoolId=\"ih2bj24l46bkgt558qsef04jeq\"/>\u003csawst:container cid=\"s:b0003tc6gnahvsfq\" xsi:type=\"sawst:section\" rendered=\"true\"/>\u003csawst:container cid=\"s:c5j314uterctfb08\" xsi:type=\"sawst:section\" rendered=\"true\"/>\u003c/sawst:container>\u003c/sawst:container>\u003c/sawst:envState>\u003c/sawst:stateRef>\u003csawst:reportXmlRefferedTo>\u003cref statePath=\"d:dashboard~p:mco0pb0nob7sqjvg~r:q4g2fiisnvk4nusv\" searchID=\"8oh8erup3kcqav10ukp36jaof2\">\u003c/ref>\u003c/sawst:reportXmlRefferedTo>\u003c/sawst:clientState></nqw>";
</script>

I want to retrieve the string ih2bj24l46bkgt558qsef04jeq under the identifier statepoolId from this script section. 我想从此脚本部分中的标识符statepoolId下检索字符串ih2bj24l46bkgt558qsef04jeq So how do I find this script in the HTML and get the string using casperjs? 那么,如何在HTML中找到此脚本并使用casperjs获取字符串?

Accessing window.sawXmlIslandidClientStateXml : 访问window.sawXmlIslandidClientStateXml

This is a script element in a page which adds a variable to the global scope, so you can simply access it through casper.evaluate : 这是页面中的脚本元素,该脚本元素将变量添加到全局范围,因此您只需通过casper.evaluate即可访问它:

casper.then(function(){
    var sawXmlIslandidClientStateXml = casper.evaluate(function(){
        return window.sawXmlIslandidClientStateXml;
    });
    // TODO: do something
});

Doing something with sawXmlIslandidClientStateXml : sawXmlIslandidClientStateXml做一些事情:

You could 你可以

  • do simple string operations to split the string in a more manageable pieces that can be parsed easier by yourself, 执行简单的字符串操作,将字符串拆分为更易于管理的片段,以便您自己轻松解析,
  • write a Regex matcher to find the specific detail you're looking for, or 编写正则表达式匹配器以查找您要查找的特定详细信息,或者
  • use a DOMParser to parse the string as XML and then use either yourParsedDocument.querySelector or yourParsedDocument.evaluate to locate the element through CSS selectors or XPath expressions, respectively. 使用DOMParser将字符串解析为XML,然后使用yourParsedDocument.querySelectoryourParsedDocument.evaluate分别通过CSS选择器或XPath表达式定位元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM