Selenium locators

We will learn about selenium locators in this blog.

Utilizing locators, one can find a certain webpage element. 

Example:
There are a username and password required when logging into a website. Therefore, you must pass the username and password values while automating. You must find the element for that. Selenium element locator techniques are crucial in these situations. There are a variety of locators, and we'll talk about them in this blog.

Visit Locator strategies | Selenium and 8 classic locators that are already embedded into selenium will be visible. You can use them to automate any website, as we'll see.

1) Class name:
Visit Freeguideex , the search link is in the upper right corner. We'll write code to make sure it is clicked. and to do that, we'll utilize a class name locator.

driver.findElement(By.className("search-expand-text")).click();

add this line of code in your class file. Make sure you have added browser launching code as instructed in previous blog.

In the above code, we have used "findElement" method. Inside this method we have used "By" class and subclass "className". Inside this subclass we have passed the class-name value. 
Right-click on search link and click on Inspect.


you'll find the element highlighted. Copy the unique class name from highlighted element and use it in the code as shown above.


In a same way, we are going to use other locators.

2) CSS selector
In the previous step we clicked on Search button, now we'll search "selenium" in the search box

Right click on search element and click on Inspect. 
As shown below you can use css selector locator. sendKey method is used to pass the selenium keyword.

driver.findElement(By.cssSelector("input[name='q']")).sendKeys("Selenium");

For classname
you can write code as follows:

driver.findElement(By.cssSelector("input.className"));

and for Id:

driver.findElement(By.cssSelector("input#ID"));

3) Name:
Now that we have pass selenium keyword, we need to hit enter button so that it will search and give appropriate result. For that we'll use Name locator.

driver.findElement(By.name("q")).submit();

here we have used name locator and submit method.

4) Class:
We'll click Show All link here.
After clicking on Inspect you will see below code.
We are going to use class here.

driver.findElement(By.class).click();

5) TagName:

driver.findElement(By.tagName("input"));

6) LinkText:

driver.findElement(By.LinkText("Login"));

7) PartiaLinkText:

driver.findElement(By.partialLinkText("Sign"));

8) Xpath:

driver.findElement(By.xpath("//div[@id='content']/input"));



Comments

Popular Posts