FreshWaterTaffy
FreshWaterTaffy

Reputation: 270

Selenium - Search for Element With Class and Text

I have the following code that will search for an element with given className and text until found or it times out. I don't like the fact that it is in an open loop for 30 seconds while returnElement == null. Is there a more efficient way of doing this? Note: It can't find the element based solely on text.

    #region FindAndWaitForElementListWithClassAndText
    public static IWebElement FindAndWaitForElementWithClassAndText(IWebDriver driver, string className, string text, int timeout = 30)
    {
        if (driver == null)
            throw new ApplicationException("No Selenium Driver defined, cannot continue.");

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeout));
        IWebElement returnElement = null;

        wait.Until(a => driver.FindElements(By.ClassName(className)));

        //Starts task that times out after specified TIMEOUT_MILLIS
        var tokenSource = new CancellationTokenSource();
        CancellationToken token =  tokenSource.Token;
        var task = Task.Factory.StartNew(() => searchForElementWtihClassAndText(driver, className, text), token);
        if(!task.Wait(TIMEOUT_MILLIS, token))
        {
            LoggerHelper.ErrorFormat("Could not find element with class and text: {0} :: {1}", className, text);
            returnElement = null;
        }

        returnElement = task.Result;

        return returnElement;
    }
    #endregion

    private static IWebElement searchForElementWtihClassAndText(IWebDriver driver, String className, String text)
    {
        IWebElement returnElement = null;

        while (returnElement == null)
        {
            var theList = driver.FindElements(By.ClassName(className));
            if (theList != null && theList.Count > 0)
            {
                foreach (IWebElement el in theList)
                {
                    if (el.Text.Equals(text, StringComparison.OrdinalIgnoreCase))
                    {
                        returnElement = el;
                        LoggerHelper.InfoFormat("Found Class Name and Text: {0} / {1}", className, text);
                    }
                }
            }
        }
        return returnElement;
    }

Here's an example element:

<div class="smtListItem smtMessageItem">
    <!-- ngIf: message.SentItem -->
    <!-- ngIf: !message.SentItem -->
    <span class="smtListName ng-binding ng-
    scope" data-ng if=
    "!message.SentItem">user08&nbsp;EIGHT</span>
    <!-- end ngIf: !message.SentItem -->

    ...
</div>

Upvotes: 3

Views: 22782

Answers (1)

Saifur
Saifur

Reputation: 16201

You can always write a xpath to find the element and check if that exists.

High level implementation can look like this

public void Test()
{
    By @by = By.XPath("//span[contains(@class,'smtListName')][contains(text(),'lastname')]");

    try
    {
        new WebDriverWait(Driver, TimeSpan.FromSeconds(30)).Until(ExpectedConditions.ElementExists(@by));
    }
    catch (NoSuchElementException exception)
    {
        Console.WriteLine(exception.Message);
    }
}

Upvotes: 7

Related Questions