成员类型'By'不是IWebElement或IList <iwebelement>

时间:2017-08-04 10:08:47

标签: c# selenium

我使用Selenium和页面对象模型模式进行了大量测试。我正在尝试传递By,因此我可以通过其名称访问元素。但是,在我的PageFactory.InitElements(excelSession, ribbon);方法中,我得到了Type of member 'By' is not IWebElement or IList<IWebElement>的例外情况。有没有办法让这个方法接受By

我的代码如下

public class ExcelRibbon
{
    [FindsBy(How = How.Name, Using = "Create")]
    [CacheLookup]
    public By Create { get; set; }
}

我收到错误的地方

public static ExcelRibbon ribbon = new ExcelRibbon();
PageFactory.InitElements(excelSession,  ribbon);
webDriverWait.Until(ExpectedConditions.ElementExists(ribbon.Create));
webDriverWait.Until(ExpectedConditions.ElementToBeClickable(excelSession.FindElement(ribbon.Create))).Click();

StackTrace

  

在   OpenQA.Selenium.Support.PageObjects.DefaultPageObjectMemberDecorator.CreateProxyObject(类型   memberType,IElementLocator locator,IEnumerable`1 bys,Boolean   高速缓冲存储器)

1 个答案:

答案 0 :(得分:1)

当您添加FindsBy属性时,它希望使用此处描述的定位器方法来定位元素。 By是一个定位器,而不是一个元素(如错误中所述)。

我不知道你的意思
  

有没有办法让这种方法接受By

您没有声明方法,而是将字段声明为By类型。将类型从By更改为IWebElement

[FindsBy(How = How.Name, Using = "Create")]
[CacheLookup]
public IWebElement Create { get; set; }

或删除装饰器并声明一个定位器

public By Create = By.Name("Create");

或创建方法,可能类似

public void Create(By locator)
{
    // do something
}