Selenium WebDriver: clicking on elements within an SVG using XPath

前端 未结 8 1280
难免孤独
难免孤独 2020-11-28 23:02

I have an SVG object with a few circle and rectangle elements. Using webdriver, I can click on the main svg object, but not any of the elements within it. The problem only s

8条回答
  •  萌比男神i
    2020-11-28 23:17

    For anyone interested, I solved this in the following ways:

    1) I was originally testing this on OSX with Firefox 17 and Selenium 2.28/29, but figured out it only works (at least for me) on Windows with Firefox 18 and Selenium 2.29

    2) interacting with SVGs with the standard:

    driver.findElement(By.xpath(YOUR XPATH)).click();
    

    doesn't work. You need to use Actions.

    3) to interact with SVG objects, the following XPath works:

    "/*[name()='svg']/*[name()='SVG OBJECT']";
    

    The SVG object being anything under the SVG element (e.g. circle, rect, text, etc).

    An example of clicking an SVG object:

    WebElement svgObject = driver.findElement(By.xpath(YOUR XPATH));
    Actions builder = new Actions(driver);
    builder.click(svgObject).build().perform();
    

    Note: you need to call the path inside the click() function; using:

    moveToElement(YOUR XPATH).click().build().perform();
    

    doesn't work.

提交回复
热议问题