Home > Net >  How to click on svg element in Selenium Java
How to click on svg element in Selenium Java

Time:04-18

I have a problem with clicking on svg element in selenium Java.

HTML code: enter image description here

I have tried next code examples but they are not helped.

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();

I did not get any exception or error. But can not click on SVG element Which solutions are exist for it?

CodePudding user response:

The desired element is a element so to click() on the element you need to induce WebDriverWait for the elementToBeClickable() and you can use either of the following locator strategies:

compatible code

  • Using cssSelector:

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div[placement='bottom'] svg"))).click();
    
  • Using xpath:

    new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@placement='bottom']//*[name()='svg']"))).click();
    

References

You can find a couple of relevant detailed discussions in:

  • Related