Handling WebElement:
Types of Web Elements:
- Textbox
(or Editbox)
- Password
- Radio
button
- Checkbox
- Link
- Button
- Image
- etc.
Text box (or) Editbox:
Common
Actions:
- Enter some value in
'Textbox':
- Clear the entered value in
'Textbox':
- Get the entered value from
'Textbox':
- Check if the textbox element
is displayed:
- Check if the textbox element
is enabled:
- Check if the textbox element
is selected:
2 Steps to Handle Textbox:
Enter "Raju" in First name textbox
- Identify the 'first name' textbox using
properties:
- What action
we are performing on element (Enter/Send
"Raju"):
package WebElementBasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class TextboxBasics {
public static void main(String[] args) throws InterruptedException {
// open chrome browser
System.setProperty("webdriver.chomre.driver", ".\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// wait for 3 sec
Thread.sleep(3000);
// open url -file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");
// Enter "Raju" in 'First name' text box using by id
// 1. Identify/find 'first name' text box Ele using properties --> id
// 2 . What action we are performing on element (Enter/ Send "Raju" )
WebElement firstNamTxtBoxEle = driver.findElement(By.id("idfirst"));
// import webele
// findElement() - find the element in browser by using id= 'idfirst'
// and returns 1st matched element
// goes to browser / html page - it will find the ele by id = "idfirst"
// if ele/ tag is found , it returns first matched webelement
// return type of findElement() is WebElement
// what action -->
firstNamTxtBoxEle.sendKeys("Raju");
// enter 'Swathi' in 'Last name' textbox using id
WebElement lastNameTxtBoxEle = driver.findElement(By.id("idfirst"));
lastNameTxtBoxEle.sendKeys("Swathi");
// it enters data in first name text box only but not last name
// it returns first matched webelement i.e first name text box
// enter 'Sowmya' in 'Last name' textbox using 'name'
WebElement lastNameTxtBoxEle2 = driver.findElement(By.name("lastname"));
lastNameTxtBoxEle2.sendKeys("Sowmya");
// if we pass invalid locator name= lastname123
// lastNameTxtBoxEle2 = driver.findElement(By.name("lastname123"));
// lastNameTxtBoxEle2.sendKeys("Sowmya");
//Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"css selector","selector":"*[name='lastname123']"}
// if ele is not found using given any By class locator, it throws NoSuchElementException
Thread.sleep(3000);
// clear the value from first name textbox using name
// firstNamTxtBoxEle.clear();
// clear the value from last name textbox using name
// lastNameTxtBoxEle2.clear();
// HW Enter sita in 'Test field:' text box by using "id"
//HW Enter sita in 'Test field:' text box by using "name"
// get val from 'last name' text box using name
String valFromFirstNameTxtbox = firstNamTxtBoxEle.getAttribute("value");
// default value Ram RajuSwathi
System.out.println("valFromFirstNameTxtbox="+valFromFirstNameTxtbox);
//
// valFromFirstNameTxtbox =
// HW Clear the value from 'Test field'
// HW Enter Raju in 'Test field ' text box
// HW get val from 'Test Field' text box
// check FirstName text box ele is displayed
boolean isDisplayed = firstNamTxtBoxEle.isDisplayed();
// true
System.out.println("isDisplayed="+ isDisplayed);
// true
// WebElement firstNamTxtBoxEle2 = driver.findElement(By.id("idfirst12345"));
// if ele is not found using any locator , throws NosuchElement Exception
// isDisplayed = firstNamTxtBoxEle2.isDisplayed();
// System.out.println("isDisplayed="+ isDisplayed);
// check FirstName text box ele is enabled
boolean isEnabled = firstNamTxtBoxEle.isEnabled();
// true
// isEnabled = true
System.out.println("isEnabled="+isEnabled);
// true
// Check ename text box is enabled or not
WebElement enameTxtBoxele = driver.findElement(By.name("myname"));
boolean enameTextboxEnabled = enameTxtBoxele.isEnabled();
// false
// enameTextboxEnabled = false
System.out.println("enameTextboxEnabled="+enameTextboxEnabled);// false
// check FirstName text box ele is Selected
// isSleected () applicable for radio button, checkboxes --***
boolean isSelected = firstNamTxtBoxEle.isSelected();
// false
System.out.println("isSelected="+isSelected);// false
// note: Dont prefer isSelected() for textboxes, dropdown, links etc
// Always prefer to use isSelected () for radio button, checkboxes --***
}
}
**************************************
28-Aug-2024
***************************************
package WebElementBasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class HandlingTextboxEleBasics1 {
public static void main(String[] args) throws InterruptedException {
//open chrome browser
int a ;
System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Thread.sleep(3000);
// open url -file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");
// check 'first name' text box is displayed
WebElement firstNameTxtBoxEle = driver.findElement(By.id("idfirst"));
boolean isDisplayed = firstNameTxtBoxEle.isDisplayed();
/// true
// isDisplayed = true
System.out.println("isDisplayed="+isDisplayed);// true
// check last name text box is displayed or not using invalid locator
// WebElement lastNameTxtboxEle = driver.findElement(By.name("lastname12345"));
// isDisplayed = lastNameTxtboxEle.isDisplayed();
// goes to browser / html page , it finds the elmeent by name = lastname12345
// if ele is found in page with name = lastname12345, it returns 1st matched element only.
// if ele is not found in page with name =lastname12345, It throws NoSuchElementException:
// we dont have any tag name where name = lastname12345
System.out.println("isDisplayed="+isDisplayed);
// HW check 'Password'text box is enabled
//
// isEnabled=
System.out.println("isEnabled=");//
//HW check Ename text box is enabled or not using name
//
System.out.println("isEnabled=");//
//note : dont use isSelected() for text boxes
//use isSelected() for radio btn, checkboxes but not for textbox
}
}
----------------
ex: By.class():
---------------
package WebElementBasics;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class HandlingTextboxEleBasics1 {
public static void main(String[] args) throws InterruptedException {
//open chrome browser
int a ;
System.setProperty("webdriver.chrome.driver", ".\\Drivers\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
Thread.sleep(3000);
// open url -file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html
driver.get("file:///C:/brahma/Practise/qtp%20practise/web%20apps/ALL%20Web%20objects.html");
// by.id() by.name() by.className
// Enter data in 'First name : ' text box by 'class' locator
WebElement firstNameTxtBoxEle = driver.findElement(By.className("firstclass"));
firstNameTxtBoxEle.sendKeys("Sita");
// Enter data in 'Test field: ' text box by class locator
WebElement testFieldTxtboxEle = driver.findElement(By.className("firstclass"));
testFieldTxtboxEle.sendKeys("Testing");
// it enters data first name texbox ele but not testFieldTxtboxEle
// findEleemt() returns 1st matched webelement only i.e first name text box
}
}
No comments:
Post a Comment