What is Locator in Selenium and How to Use?
To find web elements on web page then selenium uses locators. So we can say it is like technique/command to know address of web element on web page. Locators come with some types like:
id, name, class, Link Text, DOM, Xpath, CssSelector
Now main things is that how to use. So suppose we want to give any input in “Search Input Box” with automation script so how to find this element by using any locator.
Step 1 >> Open web page for what need to create script. For example:-
Step2 >> Press “F12” or Right click over the page and select “inspect Element/Inspect” what is appearing then like below screen will be there with inspect element section.
Step 3 >> Click on “pointer”
Step 4 >> Click on “Element” what need to find
Step 5 >> See below screen where related code is highlighted with some locators.
Step 6 >> For now we find it by locator “id”, so see the value of this element and locator.
So locator name is “id” and element address is “s”.
Now we can use this locator with value to give input in “search text box”.
Scripted line code will look like this:
driver.findElement(By.id(“s”).sendKeys(“locator”)); // sendKeys is to give input as per need.