Use of Webdriver for accessing WebElements

Posted By : Manish Kumar | 26-Dec-2017

We have different types of web elements located on a web page that a Quality Analyst needs to locate and access through code while writing an Automation Script.

 
Thus a QA must be known of these elements which can be Access forms, Radio buttons, Check buttons, dropdowns, file inputs and text fields.
 
First of all, we need to know about the selenium internal class Web Driver and keyword driver. Which need to be imported as a package to successfully create objects of the Web-elements for accessing
 
Step1: We import Webelement as below:
 
                    import org.openqa.selenium.WebDriver;  
 
Use of Webdriver: Web driver gives us the two internally defined methods to find our web elements
 
1.1 findelement(): this method will help us to find a single web-element and return as  a WebElement object
1.2 findElements(): this method will help us to find a list of elements and return all elements which match the locator criteria.
 
Step2: After all this importing and declaration we need to call the findelement() method and get an object of web element
 
Making and Implementing a Locator:
 
Method findelements() takes only one perimeter which needs to be locator of that element and in that we can use different type of locator like 
 
1. By.id(),
2. By.name(),
3. By.xpath(),
4. By.CSSSelector() etc..
 
Examples: 
 
driver.findElement(By.name("email"))
driver.findElement(By.cssSelector(".shw-pasw > i:nth-child(2)"))
driver.findElement(By.className("fa fa-sign-out"))
 
All these Id, name, XPath and selectors can be found using Firepath and Element inspection, read the HTML and CSS code given for the page and then make selectors and Xpaths accordingly.
 
Now we have done with the Navigational Steps of our element and then we need to perform the Transactional Steps to that element which includes clicking the element, clearing the field entering texts and setting booleans for our code.
 
For Deleting the existing values we use ".clear()" function
For Entering new values we use ".SendKeys("data")" function
For Clicking a  button we use ".Click()" function
 
These functions are used at the end of the code to perform specific actions for the desired element
 
Thanks, Hope this blog will help a beginner to understand basics of writing a test script.

About Author

Author Image
Manish Kumar

Manish is Adaptive and passionate to learn new technical skills, have knowledge of testing and love to play badminton in free time

Request for Proposal

Name is required

Comment is required

Sending message..