我在單擊 selenium Java 中的 svg 元素時遇到問題。
HTML 代碼:

我嘗試了下一個代碼示例,但沒有幫助。
Actions action = new Actions(webDriver);
action.click(webElement).build().perform();
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click;", webElement);
Actions action = new Actions(webDriver);
Thread.sleep(2000);
action.moveToElement(webElement).click().build().perform();
我沒有得到任何例外或錯誤。但是不能點擊 SVG 元素 有哪些解決方案呢?
uj5u.com熱心網友回復:
所需的元素是svg元素,以便在需要為elementToBeClickable()誘導WebDriverWait的元素上單擊(),您可以使用以下任一定位器策略:
硒4兼容代碼
使用cssSelector:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[placement='bottom'] svg"))).click();使用xpath:
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@placement='bottom']//*[name()='svg']"))).click();
參考
您可以在以下位置找到一些相關的詳細討論:
- 如何通過 Java 使用 XPath 和 Selenium WebDriver 單擊 SVG 元素
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/459444.html
