小编jgo*_*ode的帖子

Selenium WebDriver:使用XPath单击SVG中的元素

我有一个带有几个圆形和矩形元素的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>
Run Code Online (Sandbox Code Playgroud)

并且WebDriver尝试右键单击矩形元素(并失败)的示例:

    WebElement mapObject = driver.findElement(By.xpath("//*[name()='svg']/*[name()='rect']"));
    Actions builder = new Actions(driver);
    builder.contextClick(mapObject).perform();
Run Code Online (Sandbox Code Playgroud)

但这可以工作并返回一个值:

    driver.findElement(By.xpath("//*[name()='svg']/*[name()='rect']")).getAttribute("x");    
Run Code Online (Sandbox Code Playgroud)

当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]
Run Code Online (Sandbox Code Playgroud)

我花了一些时间研究这个问题,它似乎是Selenium和SVG的一个常见问题,但是我想知道是否有解决方法.我发现的唯一解决方案是与SVG本身进行交互,我已经可以做了.

我正在使用Selenium 2.28(并尝试使用2.29)w/Java + Firefox 17.

任何想法都非常感激.

java firefox selenium xpath webdriver

41
推荐指数
3
解决办法
5万
查看次数

标签 统计

firefox ×1

java ×1

selenium ×1

webdriver ×1

xpath ×1