Handling Button:
Common Actions:
- Click Button
- Get Attributes of Button
- Check if Button is Displayed
- Check if Button is Enabled
- Check if Button is Selected
Example Code:
package
WebelementsBasics;
import
org.openqa.selenium.By;
import
org.openqa.selenium.WebDriver;
import
org.openqa.selenium.WebElement;
import
org.openqa.selenium.chrome.ChromeDriver;
public
class ButtonBasics {
public static void main(String[]
args) throws InterruptedException
{
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
WebDriver driver =
new ChromeDriver();// openc chrome browser
// 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");
// clear, Enter some
value 'Raju' in 'first name' text box ele by using 'name'
WebElement firstNameTextboxEle
= driver.findElement(By.name("firstname"));
firstNameTextboxEle.clear();
firstNameTextboxEle.sendKeys("Raju");
// clear,Enter some
value 'Swathi' in "last name" text box ele by using name
WebElement
lastNameTextboxEle = driver.findElement(By.name("lastname"));
lastNameTextboxEle.clear();
lastNameTextboxEle.sendKeys("Swathi");
Thread.sleep(5000);
// Click 'My submit'
button by name
// driver.findElement(By.name(""));
// no name attr/ property in html tag
// so we go with 'id' locator
WebElement
mySubmitBtnEle = driver.findElement(By.id("MyButton"));
// MySubmitButton --> mySubmitBtnEle
// submitbtn
mySubmitBtnEle.click();
// HW click Login1
button
// HW
2.get attributes of 'MySubmit' button ex
type, id , value
// HW 3.
check 'Login1' btn is displayed
// HW 4.
check 'Login1' btn is enabled
// 5.
check 'MySubmit' btn is selected
// boolean isBtnSelected =
mySubmitBtnEle.isSelected();// Throws below
//Exception in thread
"main" org.openqa.selenium.StaleElementReferenceException: stale
element reference: stale element not found
// (Session info: chrome=116.0.5845.180)
// Re identify the ele-
using findElement()
// Note
: We can write above 2 stmts in single
line also
// driver.findElement(By.id("MyButton")).click();
boolean isBtnSelected
=
driver.findElement(By.id("MyButton")).isSelected();
// false
System.out.println("isBtnSelected="+isBtnSelected);
//
isBtnSelected=false
// dont prefer using isSelected() for textbox,
Button elements -> false
// use isSelected() for Radio button, checbox ele
}
}
No comments:
Post a Comment