Reputation: 105
I am just finishing a test script and accessing a fairly dynamic page. The page in question, has an element appear (generally a radio button or tick-box), which is only present if certain criteria in previous pages are met. So, my test will be accessing this page irrelevant of previous criteria and I want to hit the "continue" element at the bottom of the page whilst handling these elements "IF" they appear. I have a few method sto click elements by ID, and so far have the following code:
// Selects the "Confirm" button
IWebElement radioOption = mWebDriver.FindElement(By.Id("Radio_Button_Id"));
if (radioOption.Displayed)
{
this.ClickElementById("Radio_Button_Id");
// Clicks CONTINUE
this.ClickElementById("CONTINUE");
}
else
{
// Selects CONTINUE
this.ClickElementById("CONTINUE");
}
I am trying in this code to handle that if the radio button appears, select it then select the continue button. Also, if the radio button does not appear, ignore it and select the continue button. Any help with this would be much appreciated.
Upvotes: 7
Views: 26815
Reputation: 880
Try something like this:
//Displayed
public static bool IsElementDisplayed(this IWebDriver driver, By element)
{
IReadOnlyCollection<IWebElement> elements = driver.FindElements(element);
if (elements.Count > 0)
{
return elements.ElementAt(0).Displayed;
}
return false;
}
//Enabled
public static bool IsElementEnabled(this IWebDriver driver, By element)
{
IReadOnlyCollection<IWebElement> elements = driver.FindElements(element);
if (elements.Count > 0)
{
return elements.ElementAt(0).Enabled;
}
return false;
}
You'll not get any exception and then the test can continue.
Upvotes: 12
Reputation: 18783
I've also used this as a way to test if the element is present and get a handle on the element if it is present:
namespace SeleniumExtensions
{
public static class WebDriverExtensions
{
public static bool TryFindElement(this IWebDriver driver, By by, out IWebElement element)
{
try
{
element = driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
element = null;
return false;
}
}
public static bool IsElementEnabled(this IWebDriver driver, By by)
{
IWebElement element = null;
if (driver.TryFindElement(by, out element))
{
return element.Enabled;
}
return false;
}
}
}
This allows for code like:
using SeleniumExtensions;
// ...
IWebElement element = null;
if (driver.TryFindElement(By.Id("item-01"), out element)
{
// use element
}
else
{
// element is null
}
Or:
if (driver.IsElementEnabled(By.Id("item-01"))
{
// item is enabled
}
Upvotes: 0
Reputation: 8386
You said that you were getting NoSuchElementExceptions. radioOption.Displayed
tests to see if the element is visible on the page, but it will throw an error if the element doesn't even exist. (An element can be present, but invisible)
To test to see if an element is present, you need to do mWebDriver.FindElements
(note the S). This will return a List<WebElement>
of all of the elements that match your selector, and if it can't find any, it will return a list of size 0 (and not throw an error).
That way, your if statement will be if (radioOptions.size()!=0)
, and will check to see if the element exists (not if its visible).
Upvotes: 1