- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Clicking on elements within an SVG using XPath (Selenium WebDriver).
We can click on elements with an SVG using XPath in Selenium. The SVG element has the tag name svg. It has attributes like width, height, viewBox, and so on.
To click the element with svg, we should identify the element then utilize the Actions class. We shall first move to that element with the moveToElement method and then apply the click method on it.
Finally, to actually perform the action, we have to use the build and execute methods. To identify the svg element with xpath, there is the local-name() function available.
Let us look at the html code of a svg element.
Example
Code Implementation.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; import org.openqa.selenium.interactions.Action; import org.openqa.selenium.interactions.Actions; public class SVGElement{ public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\Users\ghs6kor\Desktop\Java\chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://www.tutorialspoint.com/index.htm"); // wait of 5 seconds driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); // identify element, enter text WebElement m= driver.findElement(By.xpath ("//*[local-name()='svg' and @data-icon='home']/*[localname()='path']")); // Action class to move and click element Actions a = new Actions(driver); a.moveToElement(m). click().build().perform(); } }
Output
- Related Articles
- How to use xPath in Selenium WebDriver to grab SVG elements?
- Find elements using Selenium WebDriver?
- Clicking an element using javascript vs actions vs webdriver?
- How to find an element using the “XPath” in Selenium?
- How to select element using XPATH syntax on Selenium for Python?
- Delete Cookies On All Domains using Selenium Webdriver.
- Unable to locate an element using xpath error in selenium-java
- How do you click on an element which is hidden using Selenium WebDriver?
- How to click on across browsers using Selenium Webdriver?
- How to select specified node within Xpath node sets by index with Selenium?
- Find elements inside forms and iframe using Java and Selenium WebDriver.
- Test if an element is focused using Selenium Webdriver.
- How to click Allow on Show Notifications popup using Selenium Webdriver?
- Using the Selenium WebDriver - Unable to launch chrome browser on Mac
- How to click on a link using Selenium webdriver in Python.

Advertisements