Handling Radio Button:
Common Actions:
- Click Radio button
- Check if 'Radio button' element is displayed
- Check if 'Radio button' element is enabled
- Check if 'Radio 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 RadioBtnBasics {
public static void main(String[]
args) throws InterruptedException {
//open chrome browser
System.setProperty("webdriver.chrome.driver",
".\\Drivers\\chromedriver.exe");
WebDriver driver =
new ChromeDriver();
// 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");
// click 'Male' radio btn using id
WebElement maleRadioBtnEle =
driver.findElement(By.id("maleid"));
maleRadioBtnEle.click();
Thread.sleep(3000);
// check "male" radio btn is selected
boolean isSelected
= maleRadioBtnEle.isSelected();
// true
//
System.out.println("isSelected="+isSelected);
//
// isSelected= true
// check 'Female' radio
btn is selected using id
WebElement
femaleRadioBtnEle= driver.findElement(By.id("femaleid"));
isSelected =
femaleRadioBtnEle.isSelected();
// isSelected2 = false
//
System.out.println("isSelected="+isSelected);//
// HW check 'Male' radio btn is displayed
// HW check 'Male' radio
btn is enabled
// get
id, type attribute values for
'male' radio btn
String attrID= maleRadioBtnEle.getAttribute("id");
// maleid
;
System.out.println("idVal="+attrID);
// maleid
///
String type=
maleRadioBtnEle.getAttribute("type");
// radio
System.out.println("type="+ type);
//
type=radio
//Hw click Female Radio
btn using id
// HW get name, id attribute values for 'female' radio
btn
}
}
No comments:
Post a Comment