无法使用selenium webdriver单击svg路径元素

时间:2016-09-22 19:40:42

标签: javascript selenium xpath selenium-webdriver ui-automation

我正致力于自动化图表数据,以下是数据的表示方式。

<g style="cursor:pointer;" clip-path="url(#highcharts-2)" transform="translate(62,10) scale(1 1)" class="highcharts-markers highcharts-series-0 highcharts-tracker">
<path d="M 811 367.23566666666665 L 816 372.23566666666665 811 377.23566666666665 806 372.23566666666665 Z" fill="#18abc9"></path>
<path d="M 731 400.91344444444445 L 736 405.91344444444445 731 410.91344444444445 726 405.91344444444445 Z" fill="#18abc9"></path>
<path d="M 651 386.432 L 656 391.432 651 396.432 646 391.432 Z" fill="#18abc9"></path>
<path d="M 570 390.61766666666665 L 575 395.61766666666665 570 400.61766666666665 565 395.61766666666665 Z" fill="#18abc9"></path>
<path d="M 490 381.09166666666664 L 495 386.09166666666664 490 391.09166666666664 485 386.09166666666664 Z" fill="#18abc9">
</path><path d="M 410 334.905 L 415 339.905 410 344.905 405 339.905 Z" fill="#18abc9"></path></g>

我编写了以下Selenium代码来点击第一个路径元素。

List<WebElement> a = driver.findElements(By.xpath("(//*[name()='svg']//*[name()='path' and contains(@fill, '#090')])[1]"));
Actions actionBuilder = new Actions(driver);
actionBuilder.click(a.get(0)).build().perform();

列表a只收到一个webelement。尝试单击时,程序会抛出错误。

  

org.openqa.selenium.WebDriverException:元素在点(904,5566.86669921875)处无法点击。其他元素会收到点击:<path d="M 801 341.00352 L 806 346.00352 801 351.00352 796 346.00352 Z" fill="#090"></path>

我在错误中提供的路径与我提供的DOM不完全相同。

1 个答案:

答案 0 :(得分:0)

错误是由于另一个路径元素与您要单击的路径重叠。我猜测路径形状奇特,Selenium试图点击它的中间并最终点击另一条路径。您可以在Actions中尝试moveToElement()并使用偏移量,直到您做对了。