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

Not able to click an svg path element using selenium webdriver

本文关键字:单击 svg 路径 元素 webdriver selenium      更新时间:2023-09-26

我正在自动化图表数据,下面是数据的表示方式。

<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代码来单击第一个path元素。

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,556.86669921875)不可点击。其他元素将收到点击:<path d="M 801 341.00352 L 806 346.00352 801 351.00352 796 346.00352 Z" fill="#090"></path>

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

此错误是由于另一个路径元素与您想要单击的路径重叠。我猜路径的形状很奇怪,Selenium试图单击它的中间,最终单击另一条路径。你可以尝试moveToElement()Actions和发挥偏移量,直到你得到它的权利。