简体   繁体   中英

How can I get IWebElement from dictionary<string, IWebElement>

I should add element to elementCache, where string => locator(eg xpath or id of element) and when I repeat some action method FindElement will be using IWebElement from dictionary


Dictionary<string, IWebElemebnt> elementCache = new();

public bool IsEnableElement(UiElement locator)
{
   if(locator.IsVisible())
   {
      return true;
   }
   return false;
}

public IWebElement FindElement(UiElement locator)
{
   if(IsEnableElement(locator)
   {
      //return IWebElement;
   }
   else
   {
      var webElement = FindElementInParent(FindParent(locator), locator.By);

      elementCache.Add(locator.ToString(), webElement);

      return webElement;  
   }
   // check dictionary if the element exists in the dictionary, then return 
   //IWebElement, otherwise find and add to the dictionary and return it
}```

You need to use locator as a key. Then you can use it to lookup in the dictionary. Sth like this :

public IWebElement IsEnableElement(string locator)
{
     if (elementCache.ContainsKey(locator ?? "") && elementCache[key].Key == locator) 
        {
           return elementCache[locator].Value;
        }
}

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