Manually selecting web page radio button - c#

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!

Related

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

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.

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()
...

Reload a Web Page Using C#

I have a web page that prompts for user input via DropDownLists in some table cells. When a selection is made the selection replaces the DropDownList so that the action can only be performed once. In the case that a change needs to be made I want to be able to click a button that reloads the page from scratch. I have Googled and Googled but I have not managed to find a way to do this.
Any advice is appreciated.
Regards.
Put a link on the page with the text "Reload" and the url the url of the page. It's perfectly valid to have a page with a link to itself.
If you don't like the link idea, use a standard Button and in the click event, use Response.Redirect to redirect to the current page.
You can set an OnClick for your button that resets each DropDownList's SelectedIndex to 0 instead of reloading the page from scratch. Alternatively, you can set a Response.Redirect([the page's url]) into the OnClick as is suggested here.

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.

default LinkButton on enter in a Master Page

I looked through and through and found no clean solution for this. A colleague of mine did find a pretty dirty solution but I don't see why the solutions in the links reference below does not work when I place it in a master page. I have a master page that basically has a textbox and a linkbutton, when I hit the enter key, the default button for the child page gets called instead.
http://weblogs.asp.net/jeff/archive/2005/07/26/420618.aspx
set linkbutton as default button for asp:panel in asp.net (Ahmad's Solution)
any insight on this please? thanks :)
I will post the dirty solution when I get to work tomorrow.
EDIT: here's the dirty (i think) solution:
string id = ctlToClick.UniqueID;
string someJavascript = //see below
EmailTextBox.Attributes.Add("OnKeyPress", someJavascript);
The javascript (placed it here so StackOverflow can format it better):
javascript:if (window.event){
if(parseInt(window.event.keyCode) == 13){
WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('" + id + "', '', true, 'Login', '', false, true));
return false;
}
}
Is this really the only solution for this?
Did you try setting the defaultButton property?
<form id="Form1"
defaultbutton="lnkNextPage"
runat="server">
Where lnkNextPage is the LinkButton control's id.
I think you need a simple javascript to control your tab/enter keys. I have run into this before an there was no way to make the default button solution work.
The idea is simple - check each key that is entered while in the text box. If the key presed is enter or tab, find the button either by jquery or document.GetElementById('id') and invoke the click event on the button.
the form's default button doesn't work in asp.net since a): there is only 1 form, and b): for different text boxes you want to be able to control which button is actually clicked (on tab/enter).
A simple javascript file solves this.
<bleepzter/>

Categories

Resources