Selenium - 按类名和文本搜索元素

3
我有以下代码,它将搜索具有给定类名和文本的元素,直到找到或超时。我不喜欢它在 returnElement == null 的开放循环中等待30秒钟。是否有更有效的方法来做这件事?注意:它不能仅根据文本找到元素。
    #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;
    }

这里是一个示例元素:

<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>

你是指具有特定类名和文本的元素吗?你能给出具体的HTML片段吗? - Saifur
当然,我添加了一个示例元素。 - FreshWaterTaffy
1
主要问题在于你没有正确使用 WebDriverWait。如果你愿意,它可以接受一个完整的断言。所以下面的 @Saifur 的答案会很好地解决这个问题。你那里有大约20行重复的代码。 - Arran
1个回答

7

您可以始终编写 xpath 来查找元素并检查其是否存在。

高级实现可能如下所示:

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);
    }
}

请提供HTML代码和堆栈跟踪信息。这应该是解决问题的方法。 - Saifur
我正在尝试这个,但似乎无法找到该元素。我在XPath方面没有太多经验:By @by = By.XPath("//div[@class = 'smtListName'][contains(text(), 'user08 Eight')]");对于HTML:`<div class="smtListItem smtMessageItem"><!-- ngIf:message.SentItem --> <!-- ngIf:!message.SentItem --><span class="smtListName ng-binding ng-scope" data-ng-if="!message.SentItem">user08 EIGHT</span><!-- end ngIf: !message.SentItem -->` - FreshWaterTaffy
它只是在30秒后超时。找不到该元素。 - FreshWaterTaffy
那么这个元素不存在吗?不过你真的需要告诉我,我提供的 xpath 是否符合你的要求。 - Saifur
1
xpath似乎有误。我正在尝试: //span[@class = 'smtListName' and normalize-space(text()) = 'user08 EIGHT'] - FreshWaterTaffy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接