Handling Basic Elements in Selenium
Handling basic elements in Selenium will discuss about how we can interact with the elements which we will use very frequently on any applications. In this video we will understand the concept of WebElement also; as we will call all the elements which are there on the web page are web elements.
In this, we will see how we will handle the below elements which are frequent in any application:
- Textbox
- Button
- Radio Button
- CheckBox etc…
To achieve this we will write one simple sample program to automate the AUT(Application Under Test). Here we will see the process in step by step. For this we need to follow the below steps:
- Install/Download any IDE(to write the code)
- Download and Add Selenium Jar files
- Create a Java Project In IDE
- Create a Class under the Project
- Write Sample Code
- Open Browser
- Navigate to the application URL
- Capture the locators of the Web Element which are there on the application
- Write the code for automating the functionality
- Execute the program.
Below is the Sample Program:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class SampleProgram { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.gecko.driver", "Path of gecko driver"); WebDriver driver = new FirefoxDriver(); driver.get("http://demo.automationtesting.in/Register.html"); driver.findElement(By.cssSelector("input[placeholder='First Name']")).sendKeys("Krishna"); driver.findElement(By.cssSelector("input[placeholder='Last Name']")).sendKeys("Kumar"); driver.findElement(By.cssSelector("#submitbtn")).click(); Thread.sleep(5000); driver.quit(); } }
In the above program we are handling some of the basic elements (i.e. Textbox and Button). For each and every webelement it is mandatory to get the locator and use the same in the code to identify that element.
We have separate API/methods to handle different types of elements. To handle textbox we have separate method and to handle the button we have separate methods. Same way we have different API to handle different web elements. We need to understand the element before interacting with it. And the selenium simulates the human interactions means how you interact with the application manually almost same way selenium also interacts with the application.
Please watch the YouTube video for this blog for better understanding.