【发布时间】:2013-01-13 13:44:54
【问题描述】:
我有一个带有一些圆形和矩形元素的 SVG 对象。使用 webdriver,我可以单击主 svg 对象,但不能单击其中的任何元素。问题似乎只在于单击(或任何鼠标交互),因为我可以使用 getAttribute() 来返回其下任何内容的宽度、ID、x/y、文本等值。
以下是 HTML 示例:
<div id="canvas">
<svg height="840" version="1.1" width="757" xmlns="http://www.w3.org/2000/svg" style="overflow: hidden; position: relative;">
<image x="0" y="0" width="757" height="840" preserveAspectRatio="none">
<circle cx="272.34" cy="132.14">
<rect x="241.47" y="139.23">
<text style="text-anchor: middle; x="272.47" y="144.11">
</svg>
</div>
还有一个 WebDriver 尝试右键单击矩形元素(但失败)的示例:
WebElement mapObject = driver.findElement(By.xpath("//*[name()='svg']/*[name()='rect']"));
Actions builder = new Actions(driver);
builder.contextClick(mapObject).perform();
但这有效并返回一个值:
driver.findElement(By.xpath("//*[name()='svg']/*[name()='rect']")).getAttribute("x");
当WebDriver出错时,通常是这样的:
org.openqa.selenium.WebDriverException: '[JavaScript Error: "a.scrollIntoView is not a function" {file: "file:///var/folders/sm/jngvd6s97ldb916b7h25d57r0000gn/T/anonymous490577185394048506webdriver-profile/extensions/fxdriver@googlecode.com/components/synthetic_mouse.js" line: 8544}]' when calling method: [wdIMouse::move]
我花了一些时间研究这个问题,这似乎是 Selenium 和 SVG 的一个常见问题,但我想知道是否有解决方法。我发现的唯一解决方案是与 SVG 本身进行交互,我已经可以做到了。
我正在使用带有 Java + Firefox 17 的 Selenium 2.28(并尝试过 2.29)。
非常感谢任何想法。
【问题讨论】:
-
可能不相关,但为什么不使用
driver.findElement(By.xpath("//svg/rect")).getAttribute("x");? -
因为为了在 Selenium 中通过 XPath 与 SVG 交互,您必须调用 name() 或 local-name() 方法。我不知道为什么,但是,没有它,它就行不通。
标签: java firefox xpath selenium webdriver