2017-08-04 29 views
-1

我已经使用Selenium和页面对象模型模式进行了大量测试。我试图通过一个By,所以我可以通过其名称访问元素。然而在我的PageFactory.InitElements(excelSession, ribbon);方法中,我得到了一个例外Type of member 'By' is not IWebElement or IList<IWebElement>。有没有办法让这种方法接受By成员类型'By'不是IWebElement或IList <IWebElement>

我的代码如下

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(); 

堆栈跟踪

在 OpenQA.Selenium.Support.PageObjects.DefaultPageObjectMemberDecorator.CreateProxyObject(类型 memberType,IElementLocator定位器,IEnumerabl e`1查探,布尔 缓存)

+0

添加的错误跟踪问题 –

+0

@ShubhamJain我添加了一个stackTrace –

回答

0

当您添加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 
} 
相关问题