簡體   English   中英

如何使用casperjs從嵌入式腳本中檢索文本?

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

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>

我想從此腳本部分中的標識符statepoolId下檢索字符串ih2bj24l46bkgt558qsef04jeq 那么,如何在HTML中找到此腳本並使用casperjs獲取字符串?

訪問window.sawXmlIslandidClientStateXml

這是頁面中的腳本元素,該腳本元素將變量添加到全局范圍,因此您只需通過casper.evaluate即可訪問它:

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

sawXmlIslandidClientStateXml做一些事情:

你可以

  • 執行簡單的字符串操作,將字符串拆分為更易於管理的片段,以便您自己輕松解析,
  • 編寫正則表達式匹配器以查找您要查找的特定詳細信息,或者
  • 使用DOMParser將字符串解析為XML,然后使用yourParsedDocument.querySelectoryourParsedDocument.evaluate分別通過CSS選擇器或XPath表達式定位元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM