Skip to main content

How to use Locators to locate Web Element

Everything that is showing on Web Page is the Web Element. Web Element is the Text Box, Button, Text Are, Radio Button, Checkbox, Link, Images, Browser Button, Text, iFrame, pop up window, pop up screen, Confirmation Box, Alert Box all are Web Element. So here i am explaining how to locate Web element by using Locators
1.  By using @id attribute 

Here i am giving an example of Facebook to find out the locators

ID Locators, Selenium Web Driver, Automation Testing
ID should be unique in the web page. If ID is unique then id is the always first choice to locate the web element.
id="email"
We can write script like driver.findElement(By.id("email"));

2. Locating an Element by Name

If ID is not in use then the next locators that can be use is Name attribute. But again Name also should be unique but Name can be use as multiple in the Web Page so be careful before taking Name as a locator.
name="email"
We can write script like driver.findElement(By.name("email"));

3. Locating an Element by LinkText

Automation Testing, Selenium Web Driver, LinkText Locators


The link name should be unique in the web page if multiple link name is there then this locators can not work
linkText="Create a Page"
We can write script like driver.findElement(By.linkText("Create a Page"));

4. Location an Element by partial LinkText


Again same as LinkText here we can select partial text but that text should be unique to locate the element.
Partial linkText="Create"
We can write script like driver.findElement(By.PartialLinkText("Create")); 

5.Location an element by Tag Name
Automation Testing, Selenium Web driver, Tag Name Locators

Tag Name locator is useful where we are locating Web element which is in group so we can use Tag name in Drop Down and Check Box

Tag Name = "select"
Select sel = new Select(driver.findElement(By.tagName("select")) 
sel.selectByValue("2")

6. Locating an Element by Class Name
Automation Testing, Selenium Web Driver, Class Name Locators

The Class Name should be unique in the web page. If multiple Class name is there then this location can not work.
Class Name ="inputtext"
We can write script like driver.findElement(By.className("inputtext")); 

7. Locating an element by CSS Selector
syntax of  CSS selector to location an elemenr
tagName[attributeName=attributeValue]
Automation Testing, selenium Web Driver, CSS selector Locator
In this example CCS selector will be
input[id=email] then script will be driver.findElement(By.cssSelector("input[id=email]"));
input[class=inputtext] then script will be driver.findElement(By.cssSelector("input[class=inputtext]"));
input[type=email] then script will be driver.findElement(By.cssSelector("input[type=email]"));
input[tabindex=1] then script will be driver.findElement(By.cssSelector("input[tabindex=1]"));
input[name=email] then script will be driver.findElement(By.cssSelector("input[name=email]")); 


8. XPath Locator is also used to locate the element. That is most important method to locate the element because if all above 7 failed to identify the web element then XPath will be used to locate an element so we will discuss regarding XPath in my next article.

Comments

Popular posts from this blog

Why Jmeter is best option for Performance Testing?

There are multiple tools in market for performance testing of any software but Jmeter is the best solution among all of them due to some reason listed below 1) Jmeter is the open source application so users who use this application will not to pay anything. 2) You can use Jmeter for performance of any web application,  Databases and Web Services (REST and SOAP APIs). 3) Jmeter have the user friendly UI component so it is very easy to use. 4) Jmeter can be used to simulate to put maximum number of concurrent users on a server, network or object to test its strength or to analyze overall performance under different load types. 5) Jmeter also use for functional testing for static( JavaScript and HTML) and dynamic( JSP, Servlets and AJAX) resources. 6) Jmeter provides Verity of graphical representations for performance report so it is easy to understand.

How You Can Test Database?

Now we know the Database Testing which you can learn in my last article. In this article we will discuss about the steps that need to following to test any Database. Database Tester should have some basic knowledge regarding SQL Command like DDL (create/drop),  DML (Insert/Delete/Update) and DCL (Grant/Revoke).

Causes of Software Defects

It is practically impossible that developer develop a bug free software and that software we can deliver without Testing. If the complexity of the software increased then there must be bugs that can harm the users who will use that software.