简体   繁体   中英

Asserter returning more than one element

I am trying to make a method that returns at the same time, two elements. This is my code:

 this.Wait.Until(ExpectedConditions.ElementExists(By.Id("mm_date_8")));
 this.Wait.Until(ExpectedConditions.ElementExists(By.Id("dd_date_8")));

 return this.Driver.FindElements(By.Id("mm_date_8"), By.Id("dd_date_8"));

But I don't know how to make it right...Can you please help me. Thank you in advance!!!

I am not sure if the following syntax is correct.

return this.Driver.FindElements(By.Id("mm_date_8"), By.Id("dd_date_8"));

instead what you can try to do is following.

List<IWebElement> elements = new List<IWebElement>();
AddElementsToList(elements, this.Driver.FindElements(By.Id("mm_date_8"));
AddElementsToList(elements, this.Driver.FindElements(By.Id("dd_date_8"));
// now in your calling method you can easily index list.
return elements;

public void AddElementsToList(List<IWebElement> elementList, IEnumberable<IWebElement> elementEnumerable)
{
    if (elementEnumerable != null && elementEnumerable.Any())
    {
        elementList.AddRange(elementEnumerable);
    }
}

Please note, I am assuming output of FindElements is IEnumerable. But if its other type of collection, the idea still remains the same.

If you must know what element belongs to what ID you can instead of creating a list can create a

Dictionary<string, IWebElement>

where string would be your idKey.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM