Radio Buttons in Selenium C# - c#

I have a form and cannot figure out how to automate the selection of a radio button.
The ID for the one which I need to click is "c100"
html code for the option I need to click:
<input id="cl00" type="radio" name="MYN" value="cl00">
c# code as follows:
driver.FindElement(By.Id("//input[#value='c100']")).Click();
Ive also tried:
IWebElement radio = driver.FindElement(By.Id("c100"));
radio.Click();
I have tried all the different ways including javascript. Can someone plase tell me what I am doing wrong?!

Change:
By.Id("//input[#value='c100']")
To:
By.Xpath("//input[#value='c100']")
Xpaths are useful for finding elements without displayed text or hidden elements.
Use http://www.w3schools.com/XPath/ for future reference.
Hope it helps!

Related

Unable to find "Button" with Selenium in C#

I have got a problem with Selenium code to find a button which has only "Value" and "type", in inspection it looks like this:
<input type="sumbit" value="login" />
Image of inspection
I tried twice but neither line worked for me.
The lines:
1st solution:
driver.FindElement(By.XPath("//button[contains(text(),'Login')]")).Click();
2nd solution:
driver.FindElement(By.ClassName("submit")).Click();
Image with ERROR MESSAGE (second line error)
Can anybody help me, or at least point out what am I missing, because its getting pretty frustrating to find a solution for such common thing, I practiced this on tutorial pages and buttons were never a problem.
Please. (Sorry for my English)
P.s.: I checked the "similar questions and I haven't found the solution.
P.s.s: Guys, there is another one which I didnt try yet but I have 3 different lines of code, do you think one of them will work:
There is drop-down list and I want to select the last thing in the list...
driver.FindElement(By.XPath("//*[contains(., 'Process Data >>')]"));
driver.FindElement(By.Id("pdatasub")).Click();
driver.FindElement(By.XPath("//div[text()='Process Data >>']")).Click();
Inspection of the Drop-down list
Code for the opening of the last "button" in the drop-down list:
driver.FindElement(By.XPath("//div[text()='Final Values']")).Click();
enter link description here
Thanks guys for help !
You are using the wrong locator :
try this instead :
driver.FindElement(By.XPath("//input[#type='submit' and #value='Login']")).Click();
or
With ExplicitWaits
new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[#type='submit' and #value='Login']"))).Click();
Your locator is wrong.
You can use this:
driver.FindElement(By.XPath("//button[#type='submit']")).Click();
or
driver.FindElement(By.XPath("//button[#value='login']")).Click();
or
driver.FindElement(By.XPath("//button[#value='login' and #type='submit']")).Click();
CSS Selector can be used as well similarly.
Also there are several possible issues:
You should add a wait before accessing that element. Otherwise you are trying to find an element while page is still not loaded. Expected conditions are the preferred way to do this with.
The element can be inside an iframe. If so you have to switch to that iframe in order to access elements inside it.

Using Selenium Webdriver to obtain the active status of an element?

We've got a new page we're automating, and some of the features coming through with the bootstrap template are causing some complications for automation.
Traditionally, with a checkbox or radio button, we use a tag to identify it:
[FindsBy(How = How.CssSelector, Using = "body [autotag='prf_rd_1']")]
private IWebElement myElement;
At which point you can use element.isSelected in Webdriver code to see if it's ticked.
However, with bootstrap devs have changed to use a button which is either active or not active, eg:
<button class="btn" autotag="prf_bt1" btn-radio="availabilityType.key" ng-model="model.availability.time" type="button"> … </button>
<button class="btn **active**" autotag="prf_bt1" btn-radio="availabilityType.key" ng-model="model.availability.time" type="button"> … </button>
(Hmm, not sure how to highly the word active in a code snippet, if anyone knows please feel free to edit)
So, now we can't look for 'isSelected', as it doesn't work like a checkbox.
Does anyone have a code sample for determining whether a button has active class in its css, without using XPATH? Currently we would use the CssSelector as above, and this way elements can move, text can change but as long as that autotag is still there, it's all good. However, I'm unclear how I could do this AND look for other CSS (the class) at the same time to determine if it's active.
If I understand you correctly, you just want a CSS selector that can locate an element that contains a specific value inside a specific attribute, in this case, it's class.
That's easy, an elaborate way to do this would be:
button[class*='active']
The *= is an attribute wildcard, allowing you to say "where A is somewhere inside that attribute's value".
To specifically look for a button that has a class that contains active and have an auto-tag attribute which is set to prf_bt1:
button[class*='active'][auto-tag='prf_bt1']
You can use either of these methods to get status of an element,
getAttribute
getCssValue
isEnabled
You can get more details on these methods at here.

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

Facebook Styled ASP.NET Buttons

I am having a problem in ASP.NET to style buttons like the Facebook Buttons, found here:
http://nicolasgallagher.com/lab/css3-facebook-buttons/
I tried using asp:Button with CssClass set, which works most of the time, except for the buttons that contain images, for example:
class="uibutton icon add"
If i use:
Button
then things look fine, however I can't connect to the C# code without using JavaScript to do the work.
I'm hoping there is something simple I am missing (I have tried using 'background' and 'background image' in CSS with no luck.
As there's only one element it should work. Could you post a JSFiddle?
Bootstrap suffers the same problem, and the only way (sort of) around it is by using a LinkButton as described here.
Looking at the link provided, it seems you can't use the icons with an <input> tag, only with <button> or <a> tags. Fortunately, <asp:LinkButton> will act the same as an <asp:Button> on the server, but render as an <a> tag on the client.
So try using <asp:LinkButton CssClass="uibutton icon add" ... > and it should work fine.

Autofill 2 Fields on a Web Page with C#

I am working on a simple web forms application with C# (Microsoft Visual C# 2010 Express).
I have two text boxes (textBox1, richTextBox1) a button (button1) and a web browser (webBrowser1) on the form. The web browser goes to a web page when I run the program. On that page there are two input fields that I want to autofill with the click of the button1 using the text in textBox1 and richTextBox1.
You can see the code of the input fields on that web page:
<input type="text" id="subject" tabindex="4" name="subject" value="">
<textarea class="composebody" tabindex="6" name="message" id="message" rows="20" cols="80"></textarea>
I know this is very simple, but I don't have much knowledge about C#. Any ideas how I can code that?
Thanks.
You need to write this code
webBrowser1.Document.GetElementById("subject").SetAttribute("value", subject.text);
webBrowser1.Document.GetElementById("msg").SetAttribute("value",message.text );
and need to call those two lines in
DocumentCompleted event of webbrowser.
Hope it helps.
I believe you are looking for the following:
subject.value = "Your info here";
This will solve the issue for your first item but the text area is a bit more tricky. You will probably need to include some HTML item inside the text area that you can write to. I was not able to find a good way to write to the textarea item easily. If possible, I would suggest using a different control.

Categories

Resources