C# Selenium - How to find out if a radio button is selected - c#

I'm trying to see if a radio button is selected for a test in Selenium.
I can click on the button no problem, but I can't read if it has been selected or not. The HTML looks like this:
radio button code
I have tried rdioBtn.selected but it always comes back as false.
I can't see a checked element.
How can I tell if it is selected or not?
Thanks

You can try by using javascript executor like below.
IJavaScriptExecutor executor = (IJavaScriptExecutor)Constants.WebDriver;
bool isChecked=(bool)executor.ExecuteScript("return document.getElementsByName('communication-pref')[0].checked;");
change the "[0]" with index of whichever radio button you are trying to test.

Related

Click event not working for Firefox but is for chrome

The click event is not firing in firefox but works ok in chrome.
The test fails with the error: "Element not found on page."
Below is the code and HTML for the button I want to click.
Browser.ElementClickById("ctl00_ContentPlaceHolderBody_lvProducts_ctrl0_ctrl1_btnAddProductToCart_input");
and inside the elementclickbyid i have:
driver.FindElement(By.Id(elementID)).Click();
HTML code is:
event
You could try working around with a Javascript click.
// declare JS executor
var executor = (IJavaScriptExecutor)Driver;
// locate the input
var input = Driver.FindElement(By.XPath("//input[#type='submit']"));
// execute JS to click
executor.ExecuteScript("arguments[0].click();", input);
I've seen cases where regular Click(); does not work across browsers -- these cases are rare, but using JS click usually works across multiple browsers when I run into this issue.
driver.findElement(By.xpath("//input[#type='submit']")).click();
i am sure you are trying to use browser class to keep your methods there, but try to use xpath not id. just use this code to click what you need. don't use page object model or anything else. don't save it in your browser class under click method. just in your main code use this code to click. and before to run it make sure that you have only one type submit. if its gonna show to you 2 types then use this code
driver.findElement(By.xpath("//input[#type='submit'][1]")).click();
number 1 says click to first submit if the button which you need second then follow the logic and change the number to 2
driver.findElement(By.xpath("//input[#type='submit'][2]")).click();
for better answer share your code class and also URL where you are trying to click button and also which element you are trying to click

Trying to click radio button option by name/value in Selenium using C#

I'm new to Selenium and am able to select a value from a drop down list but can't seem to do it for a radio button. Here's the code I use for the drop down list:
public void SelectValueById(string element, string text) {
//Get hold of the dropdown box by Name
IWebElement dropDown = commondriver.FindElement(By.Id(element));
//Place the drop down into selectElement
SelectElement clickThisitem = new SelectElement(dropDown);
//Select the Item from dropdown by Text
clickThisitem.SelectByText(text);
}
This works fine. I'd like to do the same thing with a radio button. I want to pass in the same two parameters--radio button id or xpath, and the name/value of the option to select--and have the function click the correct option.
A radio button is not considered a <select> element. it is a <input> element with the attribute type='radio'. Checking and Clicking are both sufficient. (clicking moreso sometimes due to ajax binds, etc)
IWebElement radio = commondriver.FindElement(By.Id(element));
radio.click(); // this will work
radio.check(); // so will this.

Selenium to click on a javascript button corresponding to a text

I have a lot of buttons in my web page and that too are javascript buttons. All those buttons have same TagName, but different id. But I cant use ID since I cant predict which button has to be clicked.
Selenium will search for a content (Question here) and if could find the content, then it must click on the respective button. How can it be achieved?
Any comments would be really helpful and appreciated..
This will find a button based on the displayed text on the button and click it.
var loggout = driver.FindElement(By.LinkText("Logg ut"));
loggout.Click();
Or you can change it to;
By.Id()
By.CssSelector()
By.Name()
...

Page doesn't postback after clicking OK in confirm box for Radiobuttonlist

I have a RadioButtonList with 2 items. I want a postback when any of the items is selected. I have added a confirm box for one item of RadioButtonList. But, when I click OK in the confirmbox, there is no postback and SelectedIndexChanged event is not getting fired.
AutoPostback property of RadioButtonList is set to true.
This is a code fragment from my Page_Load method:
RadioButtonOpenRestricted.Attributes.Add("AutoPostBack", "True");
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("AutoPostBack", "True");
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "javascript:return confirm('Are you sure?');");
Earlier, I had added confirm box for entire RadioButtonList and postback was working as expected. But I want the confirm box to be displayed only when user clicks on "Open Access" item.
Please help!
I tried a few things. The new code lines look like:
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "javascript:showConfirmBox(0,'" + RadioButtonOpenRestricted.ClientID + "')");
RadioButtonOpenRestricted.Items.FindByValue("Restricted Access").Attributes.Add("OnClick", "javascript:showConfirmBox(1,'" + RadioButtonOpenRestricted.ClientID + "')");
The javascript method is:
function showConfirmBox(i,id)
{
if(i==0)
{
if (confirm("Are you sure you want to provide Open Access? All existing individual permissions will be removed!")==true)
{
var ctrl1 = document.getElementById(id + "_0");
ctrl1.selected=true;
}
else
{
var ctrl2 = document.getElementById(id + "_1");
ctrl2.selected=true;
}
}
if(i==1)
{
var ctrl2 = document.getElementById(id + "_1");
ctrl2.selected=true;
}
}
The problem with this code is that it is treating both OK and Cancel as same. The confirm box is getting displayed but if-else part of the javascript method is not getting called. I tried using OnClientClick also...this doesnt even display the Confirmbox.
Help!!!
This is because your on click script does not play well with auto-post back script generated by the ASP.NET. A quick hack solution can be
RadioButtonOpenRestricted.AutoPostBack = true;
RadioButtonOpenRestricted.Items.FindByValue("Open Access").Attributes.Add("OnClick", "if (!confirm('Are you sure?')) return false;");
Although, this will still give you an issue when you cancel on your confirmation box (in such case, you have to add script to select the previous radio button again).
As such I am not a great fan of radio button list - you may consider alternate mechanism such as repeater with radio button in item template and having your own java-script for confirmation.
I think you want to use OnClientClick to show a "confirm" window.
I don't think you can have a javascript confirm window perform a postback, at least not the way your code is set up.
You should set the OnClientClick to show a confirm modal or window with an <asp:Button/> and have that button perform the postback your looking for.
Khushboo, it used to happen with me many times. The reason behind that was I was missing some closing tag somewhere in my aspx page. I used to copy my whole aspx page to some other text editor and paste all the elements one by one to my aspx page. It always solved my this problem. I am sure you must be missing some closing tag, please cross check all elements.

Manually selecting web page radio button

i want to programatically select a radio button in code. The radio button lives on an internal webpage.
I am trying so far like this,
HtmlElement rButton = b.Document.GetElementById("nameOfButton)
which i assume gets me a handle on the radio button, but i cant find a .selected = true or something similar. I need to do this because on the page, when this button is selected more values appear on the page which i then need to fill out.
edit: i think i have managed to do this using
rButton.invokemMemeber("click") - however i think i am still going down a cul de sac with this one.
i would like to add, i think that the page is using postback. if i click the radio button, a set of new options on the page appears, its one of these dynamically created text boxes i need a handle on. is this going to be impossible to get?
You could do,
var rButton = b.Document.GetElementById("nameOfButton");
if(rButton != null)
rButton.checked=true;
Hope this help.
This is what worked for me:
if (doc.GetElementById("nameOfButton") != null)
doc.GetElementById("nameOfButton").SetAttribute("checked", "true");
Good luck!

Categories

Resources