簡體   English   中英

無法使用Selenium WebDriver單擊SVG路徑元素

[英]Not able to click an svg path element using selenium webdriver

我正在致力於自動化圖表數據,下面是如何表示數據。

<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僅收到一個網絡元素。 嘗試單擊時程序將引發錯誤。

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試圖單擊它的中間位置,最后單擊了另一條路徑。 您可以在Actions嘗試moveToElement()並使用偏移量,直到正確為止。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM