How to identify the radio button in selenium, if id and name have the same value - selenium-webdriver

I have tried to make Selenium click a radio button. Selenium is unable to select any of the radio buttons because id and name for the radio buttons are the same.
I am adding the html code below:
<label for="gender">Gender</label>
<input id="gender " name="gender" value="M" type="radio"/>
Male
<input id="gender " name="gender" value="F" type="radio"/>
Female
<label class="error" for="gender"/>

Try this below code.
If you want to click on Male then use below code using xpath locator..
WebElement radio_male = driver.findElement(By.xpath("//input[#value='M']"));
radio_male.click();
OR
If you want to click Female radio button then use below code using xpath locator.
WebElement radio_female = driver.findElement(By.xpath("//input[#value='F']"));
radio_female.click();
Explanation of xpath:- Use value attribute of <input> tag.

Boolean btnSelected = odriver.findElement(By.id("enrollUserOptionsDiv")).isSelected();
if(btnSelected = true){
if(odriver.findElement(By.xpath("//input[#value=\"No\"]")).isSelected())
{
System.out.println( odriver.findElement(By.xpath("//input[#value=\"No\"]")).isSelected());
odriver.findElement(By.xpath("//input[#value=\"Yes\"]")).click();} //fails at this line

Related

How to write xpath or cssSelector for a material-ui webelement if the element disappears

Following is such an element(textbox):
<input aria-invalid="false" autocomplete="off" id="multiple-limit-tags" required="" type="text" class="MuiInputBase-input MuiOutlinedInput-input MuiAutocomplete-input MuiAutocomplete-inputFocused MuiInputBase-inputAdornedEnd MuiOutlinedInput-inputAdornedEnd" aria-autocomplete="list" autocapitalize="none" spellcheck="false" value="">
When I click in this textbox, a dropdown opens and following attribute appears
aria-activedescendant='multiple-limit-tags-option-0' (this 0 to 20)
But when I try to right-click and click anywhere, the dropdown closes.
Unable to capture them with xPath
Click on the textbox and then try using key press events and select the value.

Select value from ReactJS dropdown with Selenium Java

We have ReactJS front end application which has dropdown box, when clicked it shows elements inside it and need to select the checkbox(s), however if try to inspect the dropdown values, it shows attribute which goes off if clicked somewhere.
Below is the html:
<div class="MuiFormControl-root fullWidth">
<div class="MuiInputBase-root MuiOutlinedInput-root adornedEnd">
<input aria-invalid="false" autocomplete="off" id="checkboxes-tags-demo" placeholder="Select" type="text" class="MuiInputBase-input inputAdornedEnd" aria-autocomplete="list" autocapitalize="none" spellcheck="false" value="">
Tried below ways: But it is not working, getting no such element exception
List<WebElement> countries = driver.findElements(By.xpath("//div[#class='MuiFormControl-root MuiTextField-root MuiFormControl-fullWidth']/div"));
for(WebElement tag : countries){
if(tag.getText().trim().equals("TITLE"))
tag.click();
}
Gone through the thread:
Testing React JS dropdown with Selenium and java
Please guide.
Could solve the issue by following below steps:
Click the dropdown's arrow mark (^) to expand the Dropdown
Then user sendKeys arrow down/up in the input element of the dropdown> then ENTER key to select the value
Tab out from the field
driver.findElement(By.xpath("(//*[#alt='Expand'])[1]")).click();
driver.findElement(By.xpath("//input[#id='tags']")).sendKeys(Keys.ARROW_DOWN);
System.out.println("clicking arrow down");
sleep(5);
driver.findElement(By.xpath("//input[#id='tags']")).sendKeys(Keys.ENTER);
System.out.println("clicked enter");
sleep(2);
driver.findElement(By.xpath("//input[#id='tags']")).sendKeys(Keys.TAB);
System.out.println("tabbed out");

Radio buttons clear in angular js

I have 2 radio buttons,
radio1: Male, radio2: Female
When we click on male button, one text box appears and we can write name in it,and also when we click on female new text box appear and we can write name.
But my issue is when I click on male or female buttons, its value does not clear automatically.
How to clear values on click of radio buttons, so that everytime radio is pressed empty text box appears.
If I correctly understand the problem, think this is your answer
<input type="radio" ng-model="value" value="foo" ng-change='clearValue()'>
<input type="text" ng-model="name">
and then, in a controller:
$scope.clearValue = function() {
$scope.name = '';
}

How to check specific radio buttons are present

I need help with protractor code to check whether both radio buttons ( yes and no ) are present or not.
The HTML looks like this for yes and no radio buttons
For No radio button html code
<input required="required"
class="ng-touched ng-valid ng-valid-required ng-dirty ng-valid-parse"
name="dangerousGood" id="dangerousGoodNo" data-ng-model="SLI.dangerous"
value="false" data-ng-required="true"
ng-change="updateCommodityType('N');isSrvLvlHazmat();errorMsgOnScreen.msg2 = false;"
type="radio">
For Yes Radio button Html code
<input required="required"
class="ng-dirty ng-valid ng-valid-required ng-touched"
name="dangerousGood" data-ng-model="SLI.dangerous"
value="true" data-ng-required="true"
ng-change="updateCommodityType('Y');isSrvLvlHazmat();"
type="radio">
If it is the presence, and not visibility, you need to check, just use element.all() to find both inputs and check the count():
var checkboxes = element.all(by.name("dangerousGood"));
expect(checkboxes.count()).toEqual(2);
You can also use the by.model() locator:
var checkboxes = element.all(by.model("SLI.dangerous"));
You can also find both checkboxes separately, assert the presence/visibility and check what button is selected by default:
var yesRadioButton = $("input[name=dangerousGood]:not(#dangerousGoodNo)");
var noRadioButton = $("input#dangerousGoodNo");
expect(yesRadioButton.isDisplayed()).toBe(true);
expect(noRadioButton.isDisplayed()).toBe(true);
expect(yesRadioButton.isSelected()).toBe(true);
expect(noRadioButton.isSelected()).toBe(false);

Selecting Radio Buttons based on User Input in Selenium Webdriver

I want to be able to select a radio button based on User input. This radio button has multiple options with the same name.
<th class="radio">
<td>
<label for="form-1-input-3">
<input id="form-1-input-3" type="radio" checked="" value="true" name="enabled">
Enabled
</label>
<label for="form-1-input-4">
<input id="form-1-input-4" type="radio" value="false" name="enabled">
Disabled
</label>
If "enabled" is passed as a string, I should be able to select the first radio button that has the visible text, Enabled and if "disabled" is passed as a string, I should be able to select radio button that has visible text, Disabled.
I am having difficulty since the name of the radio button is same. The below code fails to find the element with the AND operator for Xpath. Has anyone encountered this before and have found a solution?
String enableRadioButtonXPath = "//input[contains(#id,'form-') and contains(#value, 'enabled')]";
String enableRadioButtonOption = "enabled";
String disableRadioButtonOption = "disabled";
WebElement enableRadioButton = webdriver1.findElement(By.name(enableRadioButtonOption));
enableRadioButton.click();
This logic might be useful for you.
For selecting first radio button use below locator
driver.findElement(By.xpath("//label[contains(.,'Enable')]/input")).click();
For selecting second radio button which has disable text
driver.findElement(By.xpath("//label[contains(.,'Disable')]/input")).click();
Maybe this could help:
public void selectByName (final WebDriver driver, final String status) {
final List<WebElement> radios = driver.findElements(By.name("enabled"));
for (WebElement radio : radios) {
if (radio.getText().equals(status)) {
radio.click();
}
}
}
Get the user input and assign to a variable called Usrinput
List<WebElement> radiobuttons = driver.findElements(By.name("radiobuttonname"));
for(WebElement radiobutton: radiobuttons) {
if(radiobutton.getAttribute("value").equals("Usrinput"))
radiobutton.click();
}

Resources