

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Among id, name, xpath and css, which locator should be used?
Each of the locators has some significance. If the page contains unique
attribute values, we should use them first. However if there are no unique elements, we should use css selector as it is more effective in terms of speed.
Css also has a drawback that we cannot traverse from child to parent node which means we cannot travel backward. But xpath allows this feature. Xpath is the most common locator in Selenium and performs traversal through DOM elements and attributes to identify an object.
An xpath is represented by two ways namely ‘/ ‘and ‘// ‘. A forward single slash means absolute path. Here xpath traverses direct from parent to child in DOM. Thus in absolute xpath we have to travel from the root node to the target.
Syntax −
driver.findElement(By.xpath("/html/body/div/input")).
A double forward ‘// ‘slash means relative path. Here xpath finds the matching element in every corner of DOM. It doesn't have a particular beginning point.
Syntax −
driver.findElement(By.xpath("//input[@name=’Tutorial’]")).
It is always recommended to use relative xpath rather than absolute xpath. In absolute xpath, we need to specify from the root to the desired element so if any of the attributes and its value get changed in between, then our xpath will no longer be correct.
Example
import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import java.util.concurrent.TimeUnit; public class TextMatch { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "C:\\Users\\ghs6kor\\Desktop\\Java\\chromedriver.exe"); WebDriver driver = new ChromeDriver(); String url = " https://www.tutorialspoint.com/questions/index.php"; driver.get(url); driver.manage().window().maximize(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); //identifying element with xpath driver.findElement(By.xpath("//input[@class=’gsc-input’]")).click(); driver.close(); } }
- Related Questions & Answers
- Which Measurement Unit should be used in CSS to set letter spacing
- Which equals operator (== vs ===) should be used in JavaScript?
- Which MySQL Datatype should be used for storing BloodType?
- Which equals operator (== vs ===) should be used in JavaScript comparisons
- When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?
- MySQL isn’t inserting binary data properly? Which datatype should be used?
- How to work with id locator in WebdriverIO?
- How should strace be used on Linux?
- Which function should be used to load a package in R, require or library?
- Explain the scenario in which CURSOR should be used over a standalone SELECT statement?
- When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used in C++?
- What HTML5 tag should be used for filtering search results.
- Which Eclipse Plugin should be installed to work with Cucumber?
- Which CSS property is to be used to set the speed curve of the Animation?
- When to use new operator in C++ and when it should not be used?