小编Fre*_*ffy的帖子

Selenium - 使用类和文本搜索元素

我有以下代码将搜索具有给定 className 和文本的元素,直到找到或超时。我不喜欢它在 30 秒内处于开环状态而 returnElement == null 的事实。有没有更有效的方法来做到这一点?注意:它无法仅基于文本找到元素。

    #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 …
Run Code Online (Sandbox Code Playgroud)

c# selenium timeout

1
推荐指数
1
解决办法
2万
查看次数

标签 统计

c# ×1

selenium ×1

timeout ×1