How can I use an XPath expression to locate elements with an application's SVG canvas?

273    Asked by david_2585 in QA Testing , Asked on May 3, 2024

I am currently engaged in a particular task that is related to automating a test for a Web-based application that heavily uses SVG elements for graphical representation. Describe to me how can I use the XPath expression to locate and interact with specific SVG elements such as circles or rectangles within the application’s SVG canvas. 

Answered by David WHITE

 In the context of selenium, here are the steps given:-

XPath for SVG element

To locate the circle element by using the Xpath, you can use attributes like CX, cy, r.

Interactions with SVG element

You can use this Xpath expression to locate and interact with the SVG element in Selenium or any other automation tool that supports Xpath.

Here is the example given by using Python programming language which would demonstrate how you can use the XPath expression for interacting with specific SVG elements within an SVG canvas by using selenium WebDriver:-

From selenium import webdriver

# Set the path to the ChromeDriver executable

  Chrome_driver_path = “path_to_chromedriver”

# Initialize the WebDriver

  Driver = webdriver.Chrome(executable_path=chrome_driver_path)

# Navigate to the webpage containing the SVG canvas

  Driver.get(https://example.com/svg_canvas)

# Locate and interact with a specific SVG circle element using XPath

  Circle_element = driver.find_element_by_xpath(“//svg/circle[@cx=’100’ and @cy=’100’ and @r=’50’]”)Circle_element.click()  # Click on the circle element

# Locate and interact with a specific SVG rectangle element using XPath

Rect_element = driver.find_element_by_xpath(“//svg/rect[@x=’50’ and @y=’50’ and @width=’100’ and @height=’50’]”)
Rect_element.send_keys(“Hello, SVG!”) # Type text into the rectangle element

# Close the WebDriver

Driver.quit()

Here is the example given in java programming language:-

Import org.openqa.selenium.By;
Import org.openqa.selenium.WebDriver;
Import org.openqa.selenium.WebElement;
Import org.openqa.selenium.chrome.ChromeDriver;
Public class SVGElementInteraction {
    Public static void main(String[] args) {

        // Set the path to the ChromeDriver executable

        System.setProperty(“webdriver.chrome.driver”, “path_to_chromedriver”);

        // Initialize the WebDriver

          WebDriver driver = new ChromeDriver();

        // Navigate to the webpage containing the SVG canvas

          Driver.get(https://example.com/svg_canvas);

        // Locate and interact with a specific SVG circle element using XPath

          WebElement circleElement = driver.findElement(By.xpath(“//svg/circle[@cx=’100’ and @cy=’100’ and @r=’50’]”));
          circleElement.click(); // Click on the circle element

        // Locate and interact with a specific SVG rectangle element using XPath

          WebElement rectElement = driver.findElement(By.xpath(“//svg/rect[@x=’50’ and @y=’50’ and @width=’100’ and @height=’50’]”));
          rectElement.sendKeys(“Hello, SVG!”); // Type text into the rectangle element

        // Close the WebDriver

        Driver.quit();
    }
}


Your Answer

Interviews

Parent Categories