In our previous post, we have discussed the different locators of selenium, and in this post, we are going to discuss “How we can locate Element By ID locator”.
Selenium ID Locators
ID locators are one of the most popular and famous ways to identify the elements in the web page. As per the W3C standard, every element supposes to be unique, which makes this ID locators one of the reliable locators.
ID locator is considered as one of the safest and fastest locator options and for every automation testers its the first choice when there are multiple-choice to locate the web elements.
Let’s take an example to understand how the ID locator work
Scenario:
- Launch Mozilla Firefox
- Open Gmail On Firefox Browser
- Locate the Enter Email Id Text box and enter an email Id with using the ID locator.
Code Snippet:
public class Locators { public static void main (String [] args){ // Open browser WebDriver driver = new FirefoxDriver(); // Open Application driver.get("<a href="https://www.gmail.com/" target="_blank" rel="noopener" data-saferedirecturl="https://www.google.com/url?hl=en&q=https://www.gmail.com&source=gmail&ust=1475225889620000&usg=AFQjCNGWiJEPM95P3VkSISyTSrTuSE2t5A">https://www.gmail.<wbr />com</a>"); // Locate the element using ID locator and enters test value "Software Testing Material." driver.findElement(By.id("<wbr />Email")).sendKeys("Software Testing Material"); } }
If you run the above Java program, then you will find out that Gmail will be launched in the Mozilla Firefox browser, after that it will enter the email address in the email id text box. I hope this gives you a clear understanding of how Id locator in Selenium works.
But since browsers do not make this thing mandatory that for each element the ID should be unique, developers take advantage of this that’s why for some of the elements in the web page ID is not present as a part of an attribute or due to auto-generated some elements have the same ID. Due to the mentioned issues, wen needs to use the other locators.
Leave a Reply