How to use xPath in Selenium WebDriver to grab SVG elements?
Last Updated :
19 Sep, 2024
Selenium WebDriver is a powerful tool for automating web browsers to test the functionality of web applications. However, when working with SVG elements on a webpage, it can be tricky to locate and interact with them using standard locators like ID, class, or name. In such cases, XPath comes to the rescue.
XPath is a robust locator strategy that can navigate through complex XML-like structures, including SVG elements, allowing testers to grab SVG elements in Selenium WebDriver and ensure that they are functioning as expected in the web application.
SVG ElementPrerequisite
We will be required 3 main things:
- Java Development Kit (JDK) installed.
- Browser Driver (e.g., ChromeDriver for Chrome).
- IDE like Eclipse or IntelliJ IDEA.
Dependencies for Selenium
We will be required to have dependencies for selenium, for that, we will add dependencies in the XML file.
Pom.xml
XML
<dependencies>
<!-- Selenium Java Dependency -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.21.0</version>
</dependency>
Example
Application.java
Java
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class Application {
public static void main(String[] args) {
// Set the path to the WebDriver executable (update the path as needed)
System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); // Replace with actual path
// Initialize the ChromeDriver
WebDriver driver = new ChromeDriver();
// Navigate to the web page containing the notification container
driver.get("https://www.geeksforgeeks.org/");
// Maximize the browser window
driver.manage().window().maximize();
try {
// Locate the SVG element using XPath
WebElement svgElement = driver.findElement(By.xpath("//div[@class='notification_container']//div[@class='notification-bell-icon']/svg"));
// Click on the SVG element
svgElement.click();
System.out.println("SVG element clicked successfully.");
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
} finally {
// Close the browser
driver.quit();
}
}
}
Output:
OutputConclusion
XPath is an essential strategy for locating and interacting with SVG elements in Selenium WebDriver, especially when other locator strategies fall short. SVG elements are commonly used in modern web development, and understanding how to work with them can enhance your automation testing.
By using XPath, you can reliably grab SVG elements, ensure their visibility, and validate their properties, making your testing more comprehensive and effective.
Similar Reads
Find Web Elements using Selenium WebDriver We can identify web elements in the web page using the following two tools: Developer Tools FireBug and FirePath Developer Tools - Right-click on the web page. Navigate to inspect element to find the developer's tool. Note: There are some websites where the right-click is disabled. eg. IRCTC, bankin
4 min read
How to click on hidden element in Selenium WebDriver? In Selenium WebDriver, interacting with web elements is key to automating web applications. However, certain elements on a webpage might be hidden or obscured, making it challenging to interact with them directly. In such cases, developers and testers often need to use alternative methods, such as J
3 min read
How to check if an element exists with Selenium WebDriver? Selenium is one of the most popular tools for automating web applications. Selenium is not a single tool but rather a set of tools that helps QA testers and developers to automate web applications. It is widely used to automate user interactions on a web page like filling out web forms, clicking on
7 min read
Best way to get element by XPath using JavaScript in Selenium WebDriver? Finding Selenium WebDriver elements requires the use of the XPath language, which is used to navigate XML documents. It provides a flexible and efficient method to locate website items. This article focuses on discussing how to get an element by XPath using JavaScript in Selenium WebDriver. JavaScri
3 min read
In Selenium WebDriver, use Python to obtain the WebElement's HTML source? The article focuses on discussing how to use Python to obtain the WebElement's HTML source. Prerequisites: Selenium Python Installation: Using Selenium Webdriver, one can obtain the HTML source of any WebElement. What is an HTML Source? HTML Source is described as the HTML code that underlies a cert
2 min read
How to Select Elements by Class in XPath? XPath (XML Path Language) is a powerful query language used to navigate and select elements in XML and HTML documents. One common task when working with XPath is selecting elements by their class attribute, particularly useful for web scraping and testing scenarios. In this article, we'll explore ho
4 min read