Gartner has predicted that worldwide information security spending will witness a growth of 7% in 2017, with the total amount spent reaching close to $93 billion in 2018. This in...
By: Rohit Sakhawalkar | September 2, 2015
Now a days Selenium is vastly used as web automation tool across industry. It has its own advantages whether it might be an open source tool or freedom of scripting language. Selenium WebDriver mimics the actual user operations, so it gives the actual user experience while execution.
Selenium have wide range of locators which helps to locate elements on a web page.
Today we will talk about them.
Selenium gives user options to locate elements in 9 different ways.
- Partial Linktext
- Tag Name
- Class Name
- CSS (Cascaded Spread Sheets)
- XPath (XML path)
- DOM (Data object modeling) (Not supported by WebDriver)
The sequence of the operator shows how much your script is going to be efficient while execution. To elaborate this lets take one example.
We will try to locate field User ID by Id and XPath.
Locator by id : Id= txtUserName Locator by xpath : XPath= //input[@id=’txtUserName’]
(xpath might have different combination we will see them in xpath section.)
If we execute script using both locators, to locate the field using Id will take less time compared to Xpath. In this way we will able to increase the efficiency of script by reducing execution time.
Now we will see all locators in detail.
The most preferred, the easiest and efficient way to locate an element on a web page is By ID. Id will the attribute on the page which will be unique like you bank account number or employee id. Ids are the safest and fastest locator option and should always be the first choice even when there are multiple choices.
Example 1: <input id="txtUserName" type="text">
Example 2: <input id="txtUserName" name="userName" type="text">
In first example its straight forward we only have Id, but in second we have Id as well as Name as an attribute. We can write the script as
WebElement Ele = driver.findElement(By.id("txtUserName "));
But in many cases we found that we have common Id or dynamic Ids (like in case of google, Gmail or the application using GWT). In that case we need to use different locators.
This is a fall back option when Id for element is not present. But mostly the names are used again and again, so make sure that the name is unique on the page before using it.
Example: <input id="txtUserName" name="userName" type="text"> WebElement ele= driver.findElement(By.name("userName "));
Finding an element with link text is very simple. This locator is used in case you want to locate any hyperlink only. But make sure, there is only one unique link on the web page. If there are multiple links with the same link, in such cases Selenium will perform action on the first matching element with link on page.
In above image we have three hyperlinks. If we want to locate the Forgot Password? Link. The locator will be
<a href="#">Forgot Password? </a> WebElement hyperlink = driver.findElement(By.linkText("Forgot Password?"));
We have 2 links with text Forgot Email. If we try to locate 2nd link with locator
LinkText=*Forgot Email. Selenium will locate 1st link.
In this case if we want to locate the 2nd link, we will need to use exact keyword with colon (exact:). The locator in that case will be
linkText= exact:*Forgot Email <a style="background-color: transparent;" href="#">*Forgot Email</a> WebElement hyperlink = driver.findElement(By.linkText("exact:*Forgot Email"));
Partial LinkText works same as LinkText, only difference is you can use a part of the text from link.
Example: <a href="#">Forgot Password? </a> WebElement hyperlink = driver.findElement(By. PartialLinkText ("Password"));
Tag Name we can use for the elements like drop downs, check boxed, radio buttons. Following html is for drop down with 3 values. To select that drop down we will use tagName locator.
Example: <select name="selCity" id="selCity"> <option value="none">--Select--</option> <option value="PUNE">Pune</option> <option value="ADI">Ahmedabad</option> </select> WebDriver command: Select select = new Select(driver.findElement(By.tagName("select"))); select.selectByVisibleText("Pune"); or select.selectByValue("PUNE");
This locator we can use as a fall back option for either name or Id. But the same condition applied here Class name should be unique or selenium will locate the first element present on the page with the class name we have used to locate element.
Example: <input id="txtName" class="textboxcss" tabindex="1" type="text"> WebElement classtest =driver.findElement(By.className(“textboxcss”));
We will continue with CSS selectors in next week’s blog.