首页 > 解决方案 > Puppeteer 使用 xlink:href 查找 svg

问题描述

我有以下html:

<button data-testid="likeButton">
    <svg aria-hidden="true">
        <use xlink:href="/logo"></use>
    </svg>
    <span>Like</span>
</button>

现在<use xlink:href可能是/logo或者/logo-filled

如何在 Puppeteer 中编写 xpath 表达式来获取这些?

我试过这个:

//*[name()='use'][contains(@*='logo')]

但是当我测试它时,我得到了错误:

Unable to perform XPath operation. The prefix "xlink" for attribute "xlink:href" associated with an element type "use" is not bound.

标签: htmlxmlsvgxpathpuppeteer

解决方案


改变

//*[name()='use'][contains(@*='logo')]
                             ^

//*[name()='use'][contains(@*,'logo')]
                             ^

或者

//use[starts-with(@*,'/logo')]

但关于名称空间,另请参阅XPath 如何处理 XML 名称空间?


推荐阅读