1

I have a number of tests I'm running through Selenium. Whenever one fails I just have a catch surrounding the test saying that an Element could not be found.

It seems quite vague instead I want to be able to say what element can't be found and write that to my error log. Is there a way to determine what element cannot be found?

Here is some example code I am using at the moment to catch my error.

try
{
    base.SetUp();

    webDriverWait.Until(ExpectedConditions.ElementTo‌​BeClickable(
      excelSession.FindElementByName("Blank workbook"))).Click();
    webDriverWait.Until(ExpectedConditions.ElementTo‌​BeClickable(
      excelSession.FindElementByName("Create"))).Click();
    webDriverWait.Until(ExpectedConditions.ElementTo‌​BeClickable(
      excelSession.FindElementByName("New"))).Click();
    webDriverWait.Until(ExpectedConditions.ElementTo‌​BeClickable(
      excelSession.FindElementByName("E  Sample Data"))).Click();

    CommonMethods.IsElementDisplayed(excelSession,
      new StackTrace(true).GetFrame(0).GetFileLineNumber(), 
      new StackTrace(true).GetFrame(0).GetMethod(), "CreateErrorIcon",
        "Error appeard while selecting the E Sample Data button");
}
catch (Exception)
{
    CommonMethods.ExceptionHandler("Couldn't find element", 
      new StackTrace(true).GetFrame(0).GetFileLineNumber(), 
      new StackTrace(true).GetFrame(0).GetMethod(), excelSession);
}
1
  • One option is to write the exception message to the logs. The exception message will indicate the locator used. Commented Jul 31, 2017 at 18:12

1 Answer 1

2

Declare a function that will search for elements:

Func<RemoteWebDriver, By, IWebElement> find = (driver, by) =>
{
    var elements = driver.FindElements(by);
    if (elements.Count < 1)
    {
        var msg = $"Couldn't find element {by}";
        logger.Warn(msg);
        throw new Exception(msg);
    }

    return elements.First();
};

Then use it like this:

webDriverWait.Until(
  ExpectedConditions.ElementTo‌​BeClickable(
  find(excelSession, By.Name("Blank workbook")))
).Click();
Sign up to request clarification or add additional context in comments.

8 Comments

Please give me a feedback. Does my code not working? Did I misunderstood a question? What wrong with this answer?
The idea is not bad but the implementation has issues. FindElementsByName() is not defined. OP used singular. You have logic issues in your if-else... you don't need the else if at all, just return the first element if .Count == 0. It would be much better to pass in a By locator instead of a string. I know OP used a string but that is not a good practice. By is much more flexible.
@JeffC Thank you for feedback, I've updated my answer. I'm using Selenium "3.4.0.0" and FindElementsByName is defined in OpenQA.Selenium.Remote.RemoteWebDriver class
This is a good answer however in my case I'm searching for a windows element and I'm using FindElementByName rather than the By.Name property is there a way of using your method to fit my methodology?
@CraigGallagher Please see a first version of my answer. Does it meet your requirements?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.