Selenium 在 XPath 中找不到 SVG 元素

问题描述 投票:0回答:3
testing xpath selenium junit svg
3个回答
109
投票

尝试以下 XPath 表达式:

//*[local-name() = 'svg']

(至少可以在 Chrome/FireBug 控制台上使用,尚未尝试使用 Selenium)


5
投票

问题是关于xPath的,但是如果你可以使用CSS选择器,那会更具可读性,就像这样(Java)。

WebElement image = driver.findElement(By.cssSelector("#imageholder > svg > g > image"));

0
投票

解决方法我使用,不需要修改文档的命名空间或放弃使用XPath:传递一个自定义的

namespaceresolver
,它的行为比默认值更理智和正确

document.evaluate(
    './_:g[3]/_:circle', // Example: select the <circle> element inside the 3rd <g>
    MY_SVG_ELEMENT,
    (prefix) => prefix === '_' ? context.namespaceURI : document.lookupNamespaceURI(prefix),
    XPathResult.FIRST_ORDERED_NODE_TYPE
);
© www.soinside.com 2019 - 2024. All rights reserved.