Tuesday, July 4, 2017

Selenium WebDriver - Exploring the Radio-Button element (C#)


A radio button or option button is a graphical control element that allows the user to choose only one from a predefined set of options.in this article, we will explore the main properties and methods associated with this element. 

תוצאת תמונה עבור ‪selenium logo‬‏

HTML








<form>
  <input type="radio" name="Color" value="Red" >Red Color<br>
  <input type="radio" name="Color" value="Orange"> Orange Color<br>
  <input type="radio" name="Color" value="Blue" checked> Blue Color<br>
  <input type="radio" name="Color" value="Amber" > Amber Color<br>
  <input type="radio" name="Color" value="Black">Black Color<br>
</form>

Code
foreach (var item in Firefox.FindElements(By.XPath("//input[contains(@name,'Color')]")))
{
//Determine which button is currently selected  
if (item.Selected == true)
{
sw.WriteLine(item.GetAttribute("value")+ "- Checked");
}
else
{
sw.WriteLine(item.GetAttribute("value") + "- Not Checked");
}
}
sw.Close();

Result:








Another Example:
Let’s select a specific button based on the following criteria:
  • The value of the button is "Red".
  • The button is not selected.

foreach (var item in Firefox.FindElements(By.XPath("//input[contains(@name,'Color')]")))
{
if (item.GetAttribute("value") == "Red"  &&  item.Selected == false)
{
item.Click();
}
}

Result:




No comments:

Post a Comment

My Presentations