How To Perform Multiple Radio Button Click in Selenium Webdriver - java

How to make condition to perform multiple radio button click (see image below)...?
multiple radio button click
I've tried for hours, but I got only single radio button click (see attached image)
single radio button click
List<WebElement> radiobutton = driver.findElements(By.xpath("//*[#type='radio']"));
System.out.println("Total element is " + radiobutton.size());
for (int i = 0; i < radiobutton.size(); i++) {
// if you are getting stale element exception because of the page
// reload
radiobutton = driver.findElements(By.xpath("//*[#type='radio']"));
System.out.println(radiobutton.get(i).getAttribute("value"));
// select your radio and click to go to next page
radiobutton.get(i).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//input[#id='btnCheckFare']"))
.click();
Thread.sleep(3000);
}

I saw your video.You have multiple groups of radio button and you can select one radio button in each group.
You have to use 2 loops,one for group and one inner loop for select radio button in a group.
If you have experience in programming then you can do it easily.Otherwise provide the link where we can see this functionality.

Related

how to setChecked radio button in programmatically (radio button created in dynamically ) android

I try that
radioButton.setChecked(true);
but its only work 4th radio button. I try to create radio button dynamically. I create radio buttion within for loop, then store the radio button value. Then restore the radio button value (that means, I have 4 options that time I choose 2nd option and saved it then restore it(setChecked 2nd option) ) but its only setChecked 4th option.
Create radioButton.
for (int k = 0; k < choiceElementList.size(); k++) {
if (choiceElementList.get(k).dataFormatId == 1) {
radioButton = new RadioButton(getContext());
radioButton.setText(choiceElementList.get(k).getDataFormatValue());
radioButton.setLayoutParams(params1);
radioButton.setPadding(0, 5, 0, 5);
Log.e("setid", String.valueOf(choiceElementList.get(k).getId())) ;
radioGroup.addView(radioButton);
}
}
Try to restore that
if(choiceElementList.get(k).getId() == Cons.Id){
radioButton.setChecked(true);
}
First set Ids to your RadioButtons
for (int k = 0; k < choiceElementList.size(); k++) {
if (choiceElementList.get(k).dataFormatId == 1) {
RadioButton radioButton = new RadioButton(getContext());
// Set ID to Radio Button
radioButton.setId(k);
radioButton.setText(choiceElementList.get(k).getDataFormatValue());
radioButton.setLayoutParams(params1);
radioButton.setPadding(0, 5, 0, 5);
Log.e("setid", String.valueOf(choiceElementList.get(k).getId())) ;
radioGroup.addView(radioButton);
}
}
now just use your RadioGroup to check desire RadioButton with its ID
if(choiceElementList.get(k).getId() == Cons.Id){
radioGroup.check(k); // K will be your ID Set for your desire RadioButton
}
Happy Coding...
according to this code segments, your radioButton variable only refer last created element (radiobutton). That's why it only marks fourth one. You need to get correct reference for radio button what u want.
This is because you're adding all your RadioButton in a RadioGroup. When a RadioButton is checked in a RadioGroup, the othe RadioButton will be unchecked. You can see the RadioGroup documentation say it clearly:
This class is used to create a multiple-exclusion scope for a set of radio buttons. Checking one radio button that belongs to a radio group unchecks any previously checked radio button within the same group.
Intially, all of the radio buttons are unchecked. While it is not possible to uncheck a particular radio button, the radio group can be cleared to remove the checked state.
The selection is identified by the unique id of the radio button as defined in the XML layout file.

How to enable button do verify in Selenium WebDriver?

I have one button section product, but it disables when hover mouse on section product then the button is enabled. I don't know write script do enable a button when Selenium WebDriver finds it.
#Test(priority = 4)
public void TestSelectedItem() {
driver.findElement(By.xpath("/html/body/app-root/ng-component/div/product-list/div/div/div/div/div[2]/div/div[3]/div/div[1]/a")).click();
Select drpItem = new Select(driver.findElement(By.id("pa_colors")));
drpItem.selectByIndex(2);
driver.findElement(By.className("add_to_cart_button")).click();
}
Please help me.
I will give you code in Python which do hover action on the element:
hover_company = driver.find_element_by_xpath("your XPath")
driver.execute_script("arguments[0].scrollIntoView(true);", hover_company)
hover = ActionChains(driver).move_to_element(hover_company)
hover.perform()
First two lines scroll to your element, last two lines -- do hover action on the element.
After this action -- you can click on the button. Try on.
Code in Java:
hover_company = driver.findElement(By.xpath("your XPath"));
driver.executeScript("arguments[0].scrollIntoView(true);", hover_company);
Actions hover = new Actions(driver);
hover.moveToElement(hover_company).perform();

How to check radio button for gender selection in selenium webdriver?

I want to click on radio button, if radio button is already selected then it should skip for the selection.
For more details on this visit this site, go to the Signed in option -> Create an account, then you will be able to getting the page, that is provided to the screenshot.
Refer Image:
Please Find the workable code:
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://automationpractice.com/index.php");
Thread.sleep(2000);
driver.findElement(By.xpath(".//a[#class='login']")).click();
Thread.sleep(1000);
driver.findElement(By.xpath(".//input[#id='email_create']")).sendKeys("abc123_1#yahoo.com");
Thread.sleep(1000);
driver.findElement(By.xpath(".//button[#id='SubmitCreate']")).click();
Thread.sleep(4000);
driver.findElement(By.xpath(".//input[#id='id_gender1']")).click();
Thread.sleep(1000);
You can put implicit waits as well
Try this way to check that the MR radio button is selected or not? If MR radio button is not selected then only if condition will execute.
WebElement MR = driver.findElement(By.xpath("//div/span/input[#id='id_gender1']"));
if(!MR.isSelected())
{
MR.click();
}
OR
If you want to check that the Mrs radio button is selected or not? If Mrs radio button is not selected then only if condition will execute.
WebElement Mrs = driver.findElement(By.xpath("//div/span/input[#id='id_gender2']"));
if(!Mrs.isSelected())
{
Mrs.click();
}

Selenium Java, How to check if multiple radio buttons are selected?

I am working on a framework and have tried the following and not results :
List<WebElement> rows = EarningNormal.oRdioList;
java.util.Iterator<WebElement> i = rows.iterator();
while(i.hasNext()) {
WebElement ContribIDYES = i.next();
//System.out.println(sitcodes.getText());
if(ContribIDYES.isSelected()){
TestDriver.globalProps.getHtmlReport().writeHTMLReport("Contribution ID Formulas must be set to Yes as default", "Contribution IDs must be set to YES ", "All Contribution IDs must be YES","Contribution ID's are set to YES" , "PASS", "Done");
}
else{
TestDriver.globalProps.getHtmlReport().writeHTMLReport("Contribution ID Formulas must be set to Yes as default", "Contribution IDs must be set to YES ", "All Contribution IDs must be YES", "Contribution ID's are NOT seto to YES as default", "FAILED",TestDriver.comUtil.getImageFileLoc(TestDriver.globalProps.getWebDriver()));
}
}
Hi when you want to verify if a radio button is selected or not then please pay attention that any input tag with type radio has a hidden attribute value known as selected if a radio button is selected then its value is = True and if not then its value is = null,hence on the basis of this you can easily identify which radio button is selcted or not .below find a working example for the same
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get("C:\\Users\\rajnish\\Desktop\\myradio.html");
// working with radio buttons
// first take all the radio buttons inside a list like below
List<WebElement> myradioloc = driver.findElements(By.xpath("//*[#type='radio']"));
// apply the for loop to identify/ verify if a radio button is selected or not
for(int i=0;i<myradioloc.size();i++){
System.out.println("attribut value Selected of radio button is : " + myradioloc.get(i).getAttribute("selected"));
// if radio button is selected then value of selected attribute is True else null
if( myradioloc.get(i).getAttribute("selected").equals("null")){
// as if loop will only run when value of selected attribute is null
// i.e only when radio button is not selected
myradioloc.get(i).click();
}
}

How to uncheck all radio buttons in a radio group, when a radio button is checked in Android?

I have created programmatically 5 radio groups with 4 radio buttons each. The problem is, when i first check a radio button from the first radio group and after that i check the second radio button from the same radio group, the first radio button remains checked. What can i do to have the normal behavior for all the radio buttons in the radio group?
This is my code:
radioGroup = new RadioGroup[5];
answer = new RadioButton[4];
int i = 0;
for (Question qn : questions) {
radioGroup[i] = new RadioGroup(this);
int j = 0;
for (Answer an : answers) {
if (qn.getID() == an.getQuestion_id_answer()) {
answer[j] = new RadioButton(this);
answer[j].setText(an.getAnswer());
radioGroup[i].addView(answer[j]);
j++;
}
}
linearLayout.addView(radioGroup[i]);
i++;
}
Thanks!
"Checking one RadioButton that belongs to a RadioGroup unchecks any previously checked RadioButton within the same group." Source: developer.android.com
If I understand right: You are talking about 4 RadioButtons in the same RadioGroup that refuse to uncheck themselves when another button in the same RadioGroup gets checked?
If that's the case, just give a different Resource ID to every RadioButton and that should fix it.

Categories