Not able to extract the name of the radio button from DOM using the xpath using selenium - selenium

When i pass //input[#type='radio'] in the xpath, i'm able to select all the radio buttons in the list but i am not able to select a particular radio button by it's name.
Name of the radio button is the fourth term in quotes inside the render_selected_term list.
<input type="radio" onclick="Element.show('indicator_radio_term_190');render_selected_term('190','1','275464','AQCB Number')" value="190" name="radio_190"/>
<input type="radio" onclick="Element.show('indicator_radio_term_179');render_selected_term('179','1','275464','AQCB Number (iLink)')" value="179" name="radio_179"/>
<input type="radio" onclick="Element.show('indicator_radio_term_19');render_selected_term('19','1','275464','Acceptance')" value="19" name="radio_19"/>
<input type="radio" onclick="Element.show('indicator_radio_term_148');render_selected_term('148','1','275464','Account (iLink)')" value="148" name="radio_148"/>
<input type="radio" onclick="Element.show('indicator_radio_term_206');render_selected_term('206','1','275464','Additional Non-standard Terms')" value="206" name="radio_206"/>
<input type="radio" onclick="Element.show('indicator_radio_term_220');render_selected_term('220','1','275464','Assigned Contract Manager (iLink)')" value="220" name="radio_220"/>
<input type="radio" onclick="Element.show('indicator_radio_term_12');render_selected_term('12','1','275464','Assignment')" value="12" name="radio_12"/>
<input type="radio" onclick="Element.show('indicator_radio_term_188');render_selected_term('188','1','275464','Authorized Purchasing Entity(ies)')" value="188" name="radio_188"/>
<input type="radio" onclick="Element.show('indicator_radio_term_226');render_selected_term('226','1','275464','Award (iLink)')" value="226" name="radio_226"/>
<input type="radio" onclick="Element.show('indicator_radio_term_196');render_selected_term('196','1','275464','Award Amount')" value="196" name="radio_196"/>

I would suggest you to use css selector in this case and attribute search. Be very careful when you use the following selector. The match you are looking for has to be unique.
Note: I am not sure which match you are looking for. So, I am showing you an example.
[onclick*='148']
Should match <input type="radio" name="radio_148" value="148" onclick="Element.show('indicator_radio_term_148');render_selected_term('148','1','275464','Account (iLink)')"/>

Try this:
<div id="something">
<input type="radio" onclick="Element.show('indicator_radio_term_190');render_selected_term('190','1','275464','AQCB Number')" value="190" name="radio_190"/>
<input type="radio" onclick="Element.show('indicator_radio_term_179');render_selected_term('179','1','275464','AQCB Number (iLink)')" value="179" name="radio_179"/>
IList<IWebElement> inputs = browser.FindElements(By.XPath("//div[#id='something']/input)");
foreach(var element in inputs)
{
if(element.getAttribute("name").Equals("radio_12"))
element.Click();
}

Related

Unable to get text from disabled textbox

I need to print value of a disabled text box. The below given is the source code
<div class="col-6 col-md-2 form-group mb-2" xpath="1">
<input name="SMDetailsViewModel.SoRSummaryRowID" class="form-control rum-disabled" id="SRowID" type="hidden" value="3908" tabindex="-1">
<input name="SMDetailsViewModel.ID" **value**="20445" class="form-control rum-disabled " id="SID" type="text" tabindex="-1">
</div>
The 'value' needs to get printed and it is dynamic value.
I have tried with CSS, Xpath like below
WebElement SoR=driver.findElement(By.cssSelector("#SID")); With xpath as well
String SoRID=SoR.getText();
System.out.println("Here SOR ID" +SoRID);
tried with GetAttribute as well
Working with below
WebElement SoR=driver.findElement(By.cssSelector("#SID"));
String SoRID=SoR.getAttribute("value");
//String SoRID=SoR.getText();
System.out.println("Here SOR ID" +SoRID);

Inputs and its attributes are constantly changing in Dynamic x-path in Selenium

I tried to find the x-path of a text field username and password, but it keeps on changing dynamically.I won't be able to use starts-with or contains in a findelement using x-path. Here's the HTML DOM-
<div id="contents">
<h1 style="white-space:pre; width:80px; float:left;line-height:35px;">Login</h1>
<span style="float:left; padding-top:13px; COLOR:#990033; font-weight:bold;"> Student | Parent | Faculty</span>
<div class="form-elements">
<div class="form-elements">
<div class="form-elements">
<div class="label-txt">Password</div>
<input id="rcnr2uew1m0rkikeaaniwk" type="password" style="display:none;" name="rcnr2uew1m0rkikeaaniwk"/>
<input id="ko2xs123ebqyoluh15bulu" type="password" style="display:none;" name="ko2xs123ebqyoluh15bulu"/>
<input id="cuouek4bfz41etm4hroj0r" type="password" style="display:none;" name="cuouek4bfz41etm4hroj0r"/>
<input id="u2ta3gv2o2ce0azx5plpuh" type="password" name="u2ta3gv2o2ce0azx5plpuh"/>
<input id="g03nwjuzhqnkuwgsl4q2mu" type="password" style="display:none;" name="g03nwjuzhqnkuwgsl4q2mu"/>
<input id="gddwv4z3amojk0yvoxi2v4" type="password" style="display:none;" name="gddwv4z3amojk0yvoxi2v4"/>
<input id="kxecmkho2vf1vcfb42icjr" type="password" style="display:none;" name="kxecmkho2vf1vcfb42icjr"/>
<span id="ctl04" style="color:Red;visibility:hidden;">*</span>
</div>
I tried to find the input[4] with no style.
Absolute x-path- html/body/form/div[3]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[4]
Next time how it changes-
Absolute x-path- html/body/form/div[3]/div[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[17]
id and name of the input also keeps on changing without any common trait
You can do it by locating sibling of the User name label that is displayed, i.e. without attribute style="display:none;"
User Name
"//div[contains(text(), 'User Name')]/following-sibling::input[not(#style='display:none;')]"
Password
"//div[contains(text(), 'Password')]/following-sibling::input[not(#style='display:none;')]"
Or something similar using type attribute:
//input[#type='password'][not(#style)]

Selenium: Select RadioButton in Label by Value (or Label Text)

I'm new to Selenium and want to select a RadioButton inside a group of labels, see Screenshot. The problem is, that all <input> elements have the same name! So I have to select them by the value (or by the label text?)...
I'm using the Java API for Selenium.
** As HTML **
<table width="100%" border="1">
...
<label>
<input type="radio" name="AktarmaSekli" value="SP" checked class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">Sipariş Planı</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="DF" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">DAG Fatura Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="ATR" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">ATR Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="AITM" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">AİTM Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="COC" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">CoC Bilgileri</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="Bakim" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">Bakım Faturaları</label><br>
<label>
<input type="radio" name="AktarmaSekli" value="AF" class="radio" onclick="_doClick('$Refresh', this, '_self', '#_RefreshKW_AktarmaSekli')">Araç Bilgileri</label></td></tr>
</table>
Good to know you have solution.. below one may also helps..
If you are trying to click on specific button, if it has value say 'ATR' you can build xpath (or css selector) simply //input[#value='ATR'] or //input[contains(text(),'ATR Bilgileri')]
off-course other ways also there to find required element..
Thanks,
Murali
I got it to work with the following code!!!
private void selectRadioButtonByValue(WebDriver driver, String radioGroupName, String valueToFind) {
List<WebElement> radioGroup = driver.findElements(By.name(radioGroupName));
for (int i = 0; i < radioGroup.size(); i++) {
if(radioGroup.get(i).getAttribute("value").equals(valueToFind)) {
radioGroup.get(i).click();
return;
}
}
throw new org.openqa.selenium.NotFoundException("Element wasn't found by value!");
}
In Page Object Pattern it will be:
#FindBy(xpath = "//*[contains(text(),'ATR Bilgileri')]")
WebElement radioATR;

How to render a Zend_Form_Element_MultiCheckbox form an array?

I need to render a MultiCheckbox like below:
Wanted:
<label>
<input type="checkbox" name="privacy[read_only]" value="1" /> Just for read only
</label>
<label>
<input type="checkbox" name="privacy[is_pulic]" value="1" /> Is public
</label>
How can I do that? I just can do with:
Unwanted:
<label>
<input type="checkbox" name="privacy[]" value="read_only" /> Just for read only
</label>
<label>
<input type="checkbox" name="privacy[]" value="is_pulic" /> Is public
</label>
Thanks so much for any your ideas.
If there was nothing else in your form, or you didn't mind each form element having the same format, you could use setElementsBelongTo($array) which is a method on a Zend_Form.
You might also then have to use individual checkboxes to acheive the desired markup, so this may or may not work in your scenario.

How to get Index of the FORM Element in the WebBrowser Control?

let say a site has 2 forms: one search form and the other is a registration form...
<form>
Search: <input type="text" name="s">
<input type="hidden" name="a" value="search">
<input type="submit" value="Search">
</form>
[..]website content blabla[...]
<h2>Registration</h2>
<form>
E-Mail: <input type="text" name="email">
<input type="hidden" name="a" value="reg">
<input type="submit" value="Register">
</form>
If I submit a form, I want to know to which form the clicked submit button belongs. GetElementbyId is not possible because the id is not always available. I want to get the index. Any ideas? (WebBrowser Element in VB.NET or C#)
You cant refer to Form object of Input Element e.g btn.Form.Name should work; give it a try
http://msdn.microsoft.com/en-us/library/aa703812(v=vs.85).aspx (reference to IHTMLInputElement::form Property)