【问题标题】:How can I get IWebElement from dictionary<string, IWebElement>如何从字典中获取 IWebElement<string, IWebElement>
【发布时间】:2021-12-08 00:50:04
【问题描述】:

我应该将元素添加到 elementCache,其中 string => 定位器(例如元素的 xpath 或 id)并且当我重复某些操作方法时 FindElement 将使用字典中的 IWebElement


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

【问题讨论】:

  • 什么是字典键?大概您将项目放入字典中,以便您可以通过键查找它们,对吧?
  • 字典键是xpath,是的,你是对的

标签: c# .net selenium-webdriver automation


【解决方案1】:

您需要使用定位器作为键。然后您可以使用它在字典中查找。像这样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-23
    • 2015-02-13
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 2020-06-02
    • 1970-01-01
    相关资源
    最近更新 更多