I cannot use the Firstordefault method on an Iwebelement type

Asked

Viewed 47 times

0

I am unable to use the Firstordefault method with Selenium

public IWebElement RetornaRegistro(string titulo)
{
    var trs = _driver.FindElement(By.CssSelector("table tbody tr"));
    var alvo = trs.FirstOrDefault(tr => tr.Text.Contains(titulo));

    if (alvo == null)
    {
        throw new System.ArgumentException("Erro ao buscar a tarefa cadastrada.");
    }

    return alvo;
}

Displays the following error:

Severity Code Description Project File Line Suppression State Error CS1061 'Iwebelement' does not contain a Definition for 'Firstordefault' and no Extension method 'Firstordefault' Accepting a first argument of type 'Iwebelement' could be found (are you Missing a using Directive or an Assembly Reference?) Mark7 C: Users Rodrigo.rebes source Mark7 Mark7 Pages Taskpage.Cs 33 Active

1 answer

0


This is because you are using the FindElement() in assigning the variable trs, where it only returns a single element, taking into account that the FirstOrDefault() is used to Enumerables.

FindElement():

Search for the first Iwebelement element using the past method.

FindElements():

A Readonlycollection collection of all Webelements which match the current criteria or an empty list if it does not match.

Then change the FindElement() for FintElements():

var trs = _driver.FindElements(By.CssSelector("table tbody tr"));

More information: https://seleniumhq.github.io/selenium/docs/api/dotnet/html/T_OpenQA_Selenium_IWebElement.htm

Browser other questions tagged

You are not signed in. Login or sign up in order to post.